Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:03:43+00:00 2026-05-26T00:03:43+00:00

I have set my project to accept unsafe code and have the following helper

  • 0

I have set my project to accept unsafe code and have the following helper Class to determine the size of an instance:

struct MyStruct
    {
        public long a;
        public long b;
    }

public static class CloneHelper
    {
        public unsafe static void GetSize(BookSetViewModel book)
        {
            long n = 0;

            MyStruct inst;
            inst.a = 0;
            inst.b = 0;
            n = Marshal.SizeOf(inst);
        }
}

This works perfectly fine with a struct. However as soon as I use the actual class-instance that is passed in:

public unsafe static void GetSize(BookSetViewModel book)
        {
            long n = 0;


            n = Marshal.SizeOf(book);
        }

I get this error:

Type ‘BookSetViewModel’ cannot be marshaled as an unmanaged structure;
no meaningful size or offset can be computed.

Any idea how I could fix this?
Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:03:43+00:00Added an answer on May 26, 2026 at 12:03 am

    Well, it really depends on what you mean by the “size” of an instance. There’s the size of the single object in memory, but you usually need to think about any objects that the root object refers to. That’s how much memory may be reclaimable after the root becomes eligible for garbage collection… but you can’t just add them up, as those objects may be referred to by multiple other objects, and indeed there may be repeated references even within a single object.

    This blog post shows some code I’ve used before to determine the size of the raw objects (header + fields), disregarding any extra cost due to the objects that one object refers to. It’s not something I would use in production code, but it’s useful for experimenting with how large an object is under varying circumstances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a few models in a simple django project. When I
I have Visual Studio 2003 project and I have set all of the AssemblyInfo
HI, I want to have set configuration settings for a unit test project that
I have a NetBeans project set up with a bunch of source classes and
I have created a set of default project templates and development framework libraries. I
Eclipse project, I have Subversion set to ignore the bin directory, and also within
im having a little issue here. I have MediaElement.js set on my project. It
Our current MVC project is set up to have ViewModels that encapsulate the data
I have a project which has a set of binary dependencies (assembly dlls for
I have a maven project that has a set of library dependancies that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.