Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

28 bytes for empty object/class instance?

Tags:

java

I did some benchmarks comparing performance of int vs Integer, long vs Long, empty class instance vs class instance with long value.

And I have few questions which I cannot answer myself:

  1. why instantiating of empty class or object takes 28 bytes?
  2. instantiating of Long object takes 29 bytes, primitive long 8 bytes, so why the difference to empty class is only 1 byte? what kind of optimization is JVM doing?

I used JDK1.6.0_30 on MacOS, and the code is available at https://github.com/mousator/benchmarks/blob/master/src/sk/emandem/michal/AutoboxingTypeBenchmark.java (you can checkout the whole project)

Thanks for answers!

like image 920
mousator Avatar asked Apr 29 '12 03:04

mousator


1 Answers

  1. I wouldn't trust your memory measurement technique. Just calling runtime.gc() doesn't necessarily do anything. Use a tool like MemoryMeasurer.
  2. Arrays incur overhead of their own: typically on the order of 12 bytes; eight for the object header and four for the array length, and then four bytes per array entry for a reference. (That's on 32-bit VMs.)
like image 176
Louis Wasserman Avatar answered Oct 10 '22 19:10

Louis Wasserman