This is after the Java 6 memory model. In a 32-bit JVM, the size of the Shallow object
8 bytes (object header) + total of all instance variables + padding (optional)
If the first 2 members are not added up to a multiple of 8, there will be an addition.
In 64 bit JVM, Shallow size
16 bytes (object header) + total of all instance variables + padding (optional)
I understand that this Object header is 2 words long (oracle hotspot VM)
in a 32-bit JVM, object header = 2 * 32 bits = 64 bits = 8 bytes
on a 64-bit JVM, object header = 2 * 64 bits = 128 bits = 16 bytes
But with CompressedOops, the three least significant bits are reduced, so it should go back to 8 bytes on a 64-bit JVM for heaps of less than 32 gigs.
But when I tested the layout of the object using JOL (layout of Java objects), it displays 12 bytes for the Object header.
Test code
public class App { public static void main( String[] args ) { System.out.println(System.getProperty("java.version")); System.out.println(VMSupport.vmDetails()); System.out.println(ClassLayout.parseClass(A.class).toPrintable()); } } class A { int a; }
Exit
1.8.0_05 Running 64-bit HotSpot VM. Using compressed references with 3-bit shift. Objects are 8 bytes aligned. Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] com.layout.test.jolTesting.A object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 int Aa N/A Instance size: 16 bytes (estimated, the sample instance is not available) Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
What am I missing here that adds these extra 4 bytes?