C# – Is Serialization reliable for object size estimation

c++memoryobjectserialization

I use serialization, in order to estimate the amount of memory used for an object.
I already read this, and this.
So I know that it may be better to use a profiler or sizeof (for value types).

I would like to know, what is the exact difference between the serialized object and the object in memory ? In what measure is serialization reliable for object size estimation ?

I am especially interested in C# serialization mechanisms.

Best Solution

The serialized form of data is not the same as in-memory; for example, a collection/dictionary will involve multiple objects for the items, the arrays, hash-buckets/indexes, etc - but the raw data (when serialized) will typically be just the data - so you might see less volume when serialized.

Equally, things like BinaryFormatter have to include a lot of (verbose) type metadata - but in the objects it just has a (terse) type handle in the object handle - so you might see more data in the serialized data. Likewise, the serializer (unless it is manually optimized) needs to tokenize the individual fields - but in memory this is implicit in the offset from the objects address.

So you might get a number from serialization, but it is not the same number.

To get an accurate idea of the size of an object graph is tricky. SOS might help; otherwise, create a whole shed-load of them and divide. Crude, but it might just work.