1

Searching Google and StackOverFlow comes up with a lot of references to this question. Including for example:

Ways to determine size of complex object in .NET?

How to get object size in memory?

So let me say at the start that I understand that it is not generally possible to get an accurate measurement. However I am not that concerned about that - I am looking for something that give me relative values rather than absolute. So if they are off a bit one way or the other it does not matter.

I have a complex object graph. It is made up of a single parent (T) with children that may have children and so on. All the objects in the graph are from the same base class. The childrean are in the form of List T.

I have tried both the serializing method and the unsafe method to calculate size. They give different answers but the 'relative' problem is the same in both cases.

I made an assumption that the size of a parent object would be larger than the sum of the sizes of the children. This has turned out not to be true. I calculated the size of the parent. Then summed the size of the children. In some cases this appeared to make sense but in others the sum of the children far exceeded the size determined for the parent.

So my question is: Why is my simple assumption that serializing an object can result in a size that is less that the sum of the children. The only answer I have come up with is that each serialized object has a fixed overhead (which I guess is self evident) and the sum of these can exceed the 'own size' of the parent. If that is so is there any way to determine what that overhead might be so that I can take account of it?

Many thanks in advance for any suggestions.

EDIT Sorry I forgot to say that all objects are marked serializable the serialization method is:

var bf = new BinaryFormatter();
            var ms = new MemoryStream();
            bf.Serialize(ms, testObject);
            byte[] array = ms.ToArray();
            return array.Length;
Community
  • 1
  • 1
ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50

1 Answers1

1

It will really depend on which serialization mechanism you use for serializing the objects. It's possible that it's not serializing the children elements, which is one reason why you'd see the parent size smaller than the sum of the children (possibly even smaller than each individual child).

If you want to know the relative size of an object, make sure that you're serializing all the fields of all objects in your graph.

Edit: so, if you're using the binary formatter, then you must look at the specification for the format used by that serializer to understand the overhead. The format specification is public, and can be found at http://msdn.microsoft.com/en-us/library/cc236844(prot.20).aspx. It's not very easy to digest, but if you're willing to put the time to understand it, you'll find exactly how much overhead each object will have in its serialized form.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Thanks I am convinced it is the overhead. I tested with some other binary serializers and the results directly link to different levels of overhead. As it happens I plan to change my binary serializer now to protobuf since it seems much faster and generates an easier to understand overhead. BTW the link above is coming up as not found..... – ScruffyDuck Oct 20 '12 at 11:41
  • Interesting, the link works for me. You can try http://msdn.microsoft.com/en-us/library/cc236844.aspx (without the ()) instead. – carlosfigueira Oct 20 '12 at 13:44