28 bytes for empty object/class instance?
java
Solution
- I wouldn't trust your memory measurement technique. Just calling `runtime.gc()` doesn't necessarily do anything. Use a tool like MemoryMeasurer.
- 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.)
Problem
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: - why instantiating of empty class or object takes 28 bytes? - 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!