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!
source share