Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - easy way to inspect structure of serialized object?

I have a class and I'd like to see which sub-objects occupy the most space when object from this class is serialized. Is there any nice tool/way to do it, except from looking at the code and analyzing it manually?

For example I would like the tool to say "member a occupies 20%, b 30% and member c occupies 50%".

Thanks

P.S. I found some related questions, but didn't find answer to my specific question there.

like image 899
duduamar Avatar asked Nov 13 '22 03:11

duduamar


1 Answers

The only relatively fast way to count the size of the object is to first write the whole object, count the usage (for example, write to a ByteArrayOutputStream) and then write each object that is referred to.

There are a couple of this to take into account:

  • The ObjectOutputStream does do some caching to be able to refer to a previously written object with a simple pointer. Use the reset() method to clear this cache.
  • Each new object type that is written, first has the class description. The overhead of this depends on the number of same-type objects that you are referring to.
  • There is a (small, 4-byte) overhead to initialize the ObjectOutputStream.

For the description of the protocol, read this description.

like image 130
Marc Avatar answered Nov 16 '22 02:11

Marc