views:

200

answers:

5

Are there ways at determining the total size of a complex object in .NET? This object is composed of other objects and might hold references to other complex objects. Some of the objects encapsulated by this object might be POD, others may not.

+1  A: 

Give System.Runtime.InteropServices.Marshal.SizeOf() a shot.

Matt Greer
`Marshal.SizeOf()` cannot take into account the size of subobjects.
drharris
Keep in mind it's a security critical function, so does not work in a lot of contexts unfortunately.
Matt Greer
It gives the size of the marshaled representation of the object, which isn't quite the same thing.
Pavel Minaev
+2  A: 

Tricky...

What if two objects point to the same string variable. Which one "owns" the string? It sounds to me you need to simulate a garbage collection to get a true answer. Not exactly light weight.

jdv
String is not a good example here because it is immutable.
Paul Sasik
@Paul: what does immutability have to do with this? The point is that an object may hold a reference to a string that has been given to it from the outside (e.g. a filename). Does that string "belong" to the object or not? Should it be counted as part of its size or not?
Pavel Minaev
+4  A: 

Put simply, there is no way to know. Assuming all the objects are serializable, you could use a BinaryFormatter to serialize them into a binary blob, and then read the size of that. However, this would include some metadata, and inflate the actual object size (but it would be a reasonable estimate).

For value types, you would use Marshal.SizeOf(), only it does not take into account any subobjects. It would calculate any internal reference type as the size of the pointer to it.

Reflection is another way, but would not expose any private members taking up memory. So, unless I'm mistaken, there is no way to really do this properly.

drharris
+4  A: 

What you have is not a "complex object", it's an object graph. To determine its total size, you need to walk that graph - starting with some root object, and using Reflection to enumerate fields of reference types and retrieving their values (and checking for loops, of course).

To get the size of a particular object in the graph, see this answer to a related question, but note that this is an evil hack, completely unsupported, may break (and may already be broken), and may result in a wholesale kitten genocide in heavenly spheres. That said, there is no "non-hacky" way to get a precise value, because it is an implementation detail by definition - you shouldn't have any use for it. And for the purpose of finding out the memory usage of an app during debugging/profiling, the hack is good enough.

Pavel Minaev
Thanks Pavel. I understood the complexities when I asked the question, I was just curious as to if someone had already created an object graph traversal which calculated this :)
Polaris878
A: 

Previously answered in this post:

http://stackoverflow.com/questions/1128315/find-size-of-object-instance-in-bytes-in-c

The code requires unsafe code (due to pointer operations), and looks like this:

static unsafe int CalcSize(object obj)
{
    RuntimeTypeHandle th = obj.GetType().TypeHandle;
    int size = *(*(int**)&th + 1);
    return size;
}

You also have to mark the project settings (Project->Build tab) to "Allow unsafe code".

code4life