serialization – Calculating byte-size of Java object – Education Career Blog

I am working on calculaitng the size memory used of a java object hashmap . It contains elements of different data types at runtime so no-of-elem * size-of-element is not that good an approach. The code right now does it by series of

if (x)
  do something
else if (primitives)
  lookup size and calculate

However this process is a CPU hog and in-efficient.

I am thinking of following 2 approaches instead:

  1. Serialize the object to a buffer and get the size.
  2. Look into java.lang.instrument to get the size

I am looking for anyones experience with these approaches for performance , efficiency, scaling etc OR if you know any better way.

P.S:
This is a background utility that I am building so the size need no be super accurate though it should be about correct. So I am willing to trade accuracy for performance

I am not interested in the deep-size the size of objects that are refered by this object will not be computed.

I am looking for a performance comparisons and understanding how getObjectSize() works internally ..so that I do not messup something else to improve the performance

Thanks

,

Use getObjectSize() method of the Instrumentation package.

Look here for implementation details:

,

The serialized size is definitely not the way to go, for two reasons:

  • In the standard java serialization there can be quite a lot of overhead which would add to the size.
  • It would not be any quicker than using the getObjectSize() method which we can presume will iterate over all the references, and use some kind of lookup to determine the size of the primitive values/references of an object.

If you need better performance then that really will depend on the distribution of your objects. One possiblility would be to do some random sampling of the values in your map, determine an average and calculate an estimate from this value.

For advice on how to look up a random value in a hashmap, see this question.

,

You may be interested in an article I wrote a while ago on how to calculate the memory usage of a Java object. It is admittedly aimed primarily at 32-bit Hotspot, although much of it applies in essence to other environments.

You can also download a simple agent for measuring Java object size from the same site which will take some of the hard work out of it for you and should work in 64-bit environments.

Note as others have I think mentioned that the serialised form of an object isn’t the same as its form in memory, so using serialisation isn’t suitable if you want to measure the memory footprint accurately.

Leave a Comment