SizeOf for managed types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the size
using sizeof for value types with prmitives types (like int32, etc).

Thanks
 
Salvador said:
Hi,

Does anybody know how to get the size in bytes of a managed type?

I have a hashtable and I want to check the size in bytes. I can get the
size
using sizeof for value types with prmitives types (like int32, etc).

Thanks

There is no reliable way to know the size of managed objects from within
your application code.
Why would you ever need to check the size of managed types?

Willy.
 
No, this returns the size of the object as it would be marshaled and can
only be used with value types (struct).

try this...
struct Test
{
public string s;
}

....
Test t = new Test();
t.s = "guess the object size";
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(t));

the result is 4, which in no way is the correct size of the object t.

Willy.
 
Hi,

Thanks for your answer, the problem that I am trying to solve is a memory
consumption issue. I have a simple structure with three fields, they are
populated from the database, if I dump them to the disk it consumes 300Mb but
if I load it in memory in a hashtable the memory consumption is 1.5Gb...
bizarre, so I wanted to check what is going on...

Anyway, Thanks for the answer.
Cheers
 
Three fields of what type?
Beware that .NET strings are unicode, if you load ascii strings from a DB
the size in memory will double.
Willy.
 
So Willy,

What do you recommend?

I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf(t)" give me?

SevDer
http://www.sevder.com
 
Inline

Willy.

SevDer said:
So Willy,

What do you recommend?
Nothing, as I said before there is no reliable way to know the exact size of
a managed object from within your managed code.
I have a similar problem. In my case I have a very complex object.
A few collections are also inside the object and we want to know the
impact of this object to our Web Application.

Use a CLR profiler to measure the size of your objects, of course if the
tend to be variable in size you should probably need to calculate an
average.
Does any one knows how to do it?
What will "System.Runtime.InteropServices.Marshal.SizeOf(t)" give me?

SevDer
http://www.sevder.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top