10

Is there a way of estimating (roughly) in memory object size from Serialized object size in Java

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130

2 Answers2

7

The size in memory will be usually between half and double the serializable size. The most extreme example might be the Byte which is more than 80 bytes Serialized can be 16 bytes in memory.

You can use a profiler to tell you how much memory an object uses. Another way is to use a tool based on Instrumentation.getObjectSize(object)

You might find this interesting Getting the size of an Object

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • interesting,why bytes is so different. and it is five times larger compared to in memory. Do you have any reference link? +1 – Clark Bao Aug 22 '11 at 12:33
  • 1
    Serialization uses a standard format with a header, classes encoded, and their parents. Instead of trying to read a guide on the internal representation of Java Serialization, which I don't belive is an open standard you would learn a lot more by trying it. – Peter Lawrey Aug 22 '11 at 12:39
  • What if I simply use ByteArrayOutputStream to write byte[], will it make any difference? – Clark Bao Aug 22 '11 at 12:48
  • 1
    To do that you need to how you want to encode each value/field, in which case you should be able to calculate how larger the encoded value will be. This doesn't give you any idea how large is the header, or the impact of the memory alignment of allocation. – Peter Lawrey Aug 22 '11 at 12:53
  • Forget about it. I mean anything can do to make the serialized byte array smaller. – Clark Bao Aug 22 '11 at 13:03
  • A byte array is different. I would use a DataOutputStream with writeShort or writeInt for the length, followed by the bytes sent. – Peter Lawrey Aug 22 '11 at 13:08
  • I just changed them all into the byte[]... :-) possibly it's the same. – Clark Bao Aug 22 '11 at 13:14
  • 1
    A byte[] uses about 12 bytes plus the `length`. Most JVMs are 8-byte aligned so you need to round up to the next 8 bytes. – Peter Lawrey Aug 22 '11 at 13:23
  • Your post of array as object is interesting. You can just post your question again with another similiar topic and rephrase it.right? :-) – Clark Bao Aug 22 '11 at 13:36
  • Yes. Are you the original poster. I thought you were already asking a slightly related question. – Peter Lawrey Aug 22 '11 at 13:38
  • When you said "You can just post your question again with another similiar topic and rephrase it.right?" I thought you were talking about yourself, my misunderstanding. – Peter Lawrey Aug 23 '11 at 07:50
  • The Object Serialization Specification, including the protocol, is a well-known Java document. Not sure what being an 'open standard' has to do with anything here. – user207421 Aug 02 '16 at 12:35
  • You say "The size in memory will be usually between half and double the serializable size." What are the conditions where the memory size is double that of serializable size? – krumpelstiltskin Jun 19 '18 at 09:36
  • 1
    @krumpelstiltskin A simple example is a String which uses UTF-8 encoding when serialized. A long ASCII chars string will be 1 byte per char (plus a fixed size header), however in memory it will be one char (2 bytes) per char (unless compressing strings are used) – Peter Lawrey Jun 22 '18 at 19:08
0

A very nice Tool for this challenge: https://github.com/jbellis/jamm

From the readme.txt:

MemoryMeter is as accurate as java.lang.instrument.Instrumentation.getObjectSize, which only claims to provide "approximate" results, but in practice seems to work as expected.

MemoryMeter uses reflection to crawl the object graph for measureDeep. Reflection is slow: measuring a one-million object Cassandra Memtable (that is, 1 million children from MemoryMeter.countChildren) took about 5 seconds wall clock time.

user85155
  • 1,370
  • 16
  • 24