Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the memory footprint of an object at Runtime in .NET?

Tags:

c#

.net-3.5

I have a static object at runtime that is basically a list of other objects (ints, strings, Dictionary, other objects, etc). Is there way to determine the memory used by my static "list of other objects" object at runtime? This would be handy for instrumentation and reporting purposes.

like image 218
Bullines Avatar asked Oct 21 '08 20:10

Bullines


1 Answers

Sizeof can be used on value types there is also Marshal.SizeOf which can be used with some hints to .NET:

http://www.pixelicious.net/2008/07/03/exception-trying-to-get-the-size-of-a-c-class-using-marshalsizeof

But... that isn't exactly the total cost since the runtime does allocate extra bytes for classes for things like sync blocks.

If you are really interested in measuring this type of thing, however, you should use the profiling API:

http://msdn.microsoft.com/en-us/library/ms404386.aspx

Or a free tool like windbg that can do all sorts of wonderful things.

like image 56
jezell Avatar answered Oct 23 '22 14:10

jezell