Video in progress
What is inside an object header: what do the mark word and the class pointer do?
In 64-bit HotSpot the header is an 8-byte mark word and a (compressed) 4-byte class pointer. The mark word holds the identity hash, the GC age and the lock state bits; the class pointer says which class the object belongs to. Arrays have an extra length field.
📖 Answer
Every Java object starts with a header before its data.
Mark word (8 bytes) is multi-purpose, and its contents change with the state:
identityHashCode: computed the first timehashCode()(when not overridden) is called, and written here;- GC age: 4 bits, which is why
MaxTenuringThresholdcan't go above 15; - lock bits: the object is unlocked, lightly locked or has a full (inflated) monitor. With a monitor, the mark word holds a pointer to the monitor object;
- during GC: the new address of a relocated object (forwarding).
Class pointer (4 or 8 bytes) points to the class metadata in metaspace. Virtual calls, instanceof and casts all go through it. It is 4 bytes when compressed class pointers are on.
Array length (4 bytes) exists only in arrays.
That is why new Object() is a 12-byte header + 4 bytes of padding = 16 bytes.
To look at it, use JOL:
Object o = new Object();
System.out.println(ClassLayout.parseInstance(o).toPrintable());
o.hashCode();
System.out.println(ClassLayout.parseInstance(o).toPrintable()); // the hash appeared in the mark word
The future: Project Lilliput's compact object headers fit the mark word and the class pointer into 8 bytes (experimental in JDK 24, a product flag in JDK 25 but not yet the default: -XX:+UseCompactObjectHeaders). In applications with many small objects the heap can shrink by 10-20%.
A useful connection for interviews: synchronized and the identity hash compete for the same mark word, so on an object whose hash has been taken, the locking mechanism works differently.
Sign up to mark studied questions and save your progress.