28 bytes for an empty instance of an object / class?

I did some tests comparing the performance of int vs Integer, long vs Long, an empty instance of a class, and an instance of a class with a large value.

And I have a few questions that I cannot answer myself:

  • why does creating an instance of an empty class or object take 28 bytes?
  • an instance of a Long object takes 29 bytes, primitive long 8 bytes, so why is the difference in an empty class only 1 byte? What kind of optimization does the JVM work?

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 check the whole project)

Thanks for answers!

+6
source share
1 answer
  • I do not trust your memory measurement technique. Just calling runtime.gc() not necessarily anything. Use a tool like MemoryMeasurer .
  • Arrays carry overhead: usually about 12 bytes; eight for the title of the object and four for the length of the array, and then four bytes to write the array for reference. (This is on 32-bit virtual machines.)
+2
source

Source: https://habr.com/ru/post/914438/


All Articles