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:
- Serialization includes the type’s full name and other rubbish unrelated to what’s in memory.
- The object might not support serialization.
- 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.
- 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.