Object Tree Size

Eliyahu Baker wrote an article about estimating the size of an object in memory. He asked what’s wrong with the code he presented:

if (graph == null)
return 0;
return Serialize(graph).Length;

He used binary serialization, to get the best estimate.

So here are a few things that are wrong with using this to estimate an object’s size:

  1. Serialization includes the type’s full name and other rubbish unrelated to what’s in memory.
  2. The object might not support serialization.
  3. It’s not an estimate of an object, but of a graph. You might want to do just this, but #2 still looms around since one of the objects in the graph might not be serializable.
  4. Custom serialization might not write all the data held in the object, but only its state. A Customer object might represent a customer from the database. The only thing that is needed in that serialization is the customer’s id. What is actually in memory might be cached properties, etc.

Eliyahu also wrote what his original intent was: Speed. When estimating the size of the object requires full serialization, it could take ‘ages’ to do it.

Advertisements

One thought on “Object Tree Size

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s