HighTechTalks DotNet Forums  

How to know a class 's size?

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss How to know a class 's size? in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Kevien Lee
 
Posts: n/a

Default How to know a class 's size? - 12-11-2006 , 11:37 PM






If there are some int,string ,ushort filed in a class,How to find out
how it would cost of the memery?


Reply With Quote
  #2  
Old   
Oliver Sturm
 
Posts: n/a

Default Re: How to know a class 's size? - 12-12-2006 , 05:08 AM






Hello Kevien,

Quote:
If there are some int,string ,ushort filed in a class,How to find out
how it would cost of the memery?
Well, there's "sizeof" in C#, but it works only on value types. Explained
here:

http://msdn.microsoft.com/library/de...rfsizeofpg.asp

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

http://msdn2.microsoft.com/en-us/lib...al.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
--
http://www.sturmnet.org/blog


Reply With Quote
  #3  
Old   
Tomaz Koritnik
 
Posts: n/a

Default Re: How to know a class 's size? - 12-12-2006 , 10:03 AM



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

"Kevien Lee" <ironpythonster (AT) gmail (DOT) com> wrote

Quote:
If there are some int,string ,ushort filed in a class,How to find out
how it would cost of the memery?




Reply With Quote
  #4  
Old   
shanemjtownsend@hotmail.com
 
Posts: n/a

Default Re: How to know a class 's size? - 12-14-2006 , 05:26 AM




Kevien Lee wrote:

Quote:
If there are some int,string ,ushort filed in a class,How to find out
how it would cost of the memery?
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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.