Groups | Blog | Home
all groups > dotnet performance > december 2006 >

dotnet performance : How to know a class 's size?


Kevien Lee
12/11/2006 8:37:35 PM
If there are some int,string ,ushort filed in a class,How to find out
how it would cost of the memery?
Oliver Sturm
12/12/2006 12:00:00 AM
Hello Kevien,

[quoted text, click to view]

Well, there's "sizeof" in C#, but it works only on value types. Explained
here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfsizeofpg.asp

For unmanaged types there's also Marshal.SizeOf, here:

http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.marshal.sizeof.aspx

Now, in the managed world there's really no reason to ask for the size of
class structures, and the size of a class is not really a defined value.
Of course you could use the C# sizeof to calculate the size of a class as
the sum of the sizes of its (value type) members, but whether or not that
would help you depends mostly on the problem you're trying to solve here.
Maybe we can help you more if you explain what it is that you're trying to
do.


Oliver Sturm
--
Tomaz Koritnik
12/12/2006 4:03:32 PM
It would probably be impossible to get the amount of memory accupied by a
class since it depends on framework implementation. What you can do is using
statistics to estimate this amount by creating lot's of classes and divide
memory consumption by number of created classes. You can measure memory
consumption with some memory usage statistics application.

regards
Tomaz

[quoted text, click to view]

Gabriel Lozano-Morán
12/13/2006 12:18:36 AM
I assume that you mean an instance of that class (object) and that you want
to know how much memory will be consumed by this object. It depends also on
how the fields are initialized during construction.

So assume that you have an object with a int (4 bytes), string, ushort (2
bytes) properties that are initialized to their default values then your
object would occupy approximately 14 bytes. If you initialize the string
field to "hello world" that would add an additional 40 bytes (not exact
science there) therefore making the size of the object 54 bytes.
Unfortunately it is not that simple. For more information read the following
blog post:
http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx

And one way to approximately calculate the size of an object (but then again
not accurate):
http://blogs.msdn.com/mab/archive/2006/04/24/582666.aspx

Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net

[quoted text, click to view]

shanemjtownsend NO[at]SPAM hotmail.com
12/14/2006 2:26:47 AM

[quoted text, click to view]

Hi Kevien

I have a similar problem and as you can see from the other posts, the
sizeof function only works on value types, so not much use for objects.

Personally I just serialize the object to either a temp file (or
memory) and check the stream pointer or file length afterwards (which
is your object size!). It's a bit clunky but it works.

Hope this helps

Shane
AddThis Social Bookmark Button