skip to content

On a 64-bit JDK 25 HotSpot JVM, what do an object's mark word and class word hold, and what does an array header add?

level: middleimportance: should knowfreq 45%

answer

  1. hash, age, lock bits
  2. hash written lazily, 31 bits
  3. two lock bits, four meanings
  4. class word points at metadata
  5. 4-byte length after the class word

basics

~20 s

The 8-byte mark word holds a lazily written 31-bit identity hash, 4 GC age bits, a self-forwarded bit and 2 lock bits; the class word points at the class metadata; an array adds a 4-byte length, so new int[0] is 16 bytes.

solid answer

~40 s

On JDK 25 with default settings the **mark word** is 8 bytes: a 31-bit identity hash, written only when the identity hash is first requested, 4 bits of GC age, one self-forwarded bit the collectors use, two lock bits, and unused bits (4 of them reserved for Valhalla). The lock bits read `01` unlocked, `00` thin-locked by lightweight locking with the rest of the header left in place, `10` inflated to a monitor, and `11` marked by the GC; biased locking was removed in JDK 18 and has no bits any more. The **class word** is a 4-byte compressed pointer to the class's metadata, which gives the JVM the object's type, size and reference slots. An array adds a 4-byte **length** right after the class word, so `new int[0]` is exactly 16 bytes.

code

java · 14 lines
java
import org.openjdk.jol.info.ClassLayout;

public class MarkWordDemo {
    public static void main(String[] args) {
        Object o = new Object();
        // hash bits still zero: no identity hash has been requested yet
        System.out.println(ClassLayout.parseInstance(o).toPrintable());

        int h = System.identityHashCode(o);
        // the mark word now carries the 31-bit identity hash
        System.out.println(Integer.toHexString(h));
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}

go deeper

for a junior

Know the three things the mark word tracks, identity hash, GC age and lock state, and that arrays carry an extra length field.

for a middle

Lay out the mark word's fields and sizes, the four lock-bit meanings under lightweight locking, the lazy hash, and the array length word with new int[0] at 16 bytes.

for a senior

Use the layout to reason about behaviour: why the hash survives lock inflation, why biased-locking folklore is obsolete, and what changes when compact headers move the class into the mark word.

for a principal

Judge which header facts are stable contract and which are release details, and keep teams from building tooling or tuning folklore on bits that have changed across JDK releases.

## Three parts, two for every object On a 64-bit HotSpot JVM running JDK 25 with default settings, every object begins with an **object header**: - an 8-byte **mark word**, the object's own runtime state; - a 4-byte **class word**, a compressed pointer to the class's metadata; - for arrays only, a 4-byte **length** after the class word. Everything below describes that default mode. Compact object headers, an opt-in layout on JDK 25, change the picture and are noted at the end. ## The mark word, bit by bit HotSpot's `markWord.hpp` lays out the 64-bit mark word of a normal object like this, most significant bits first: | Bits | Field | What it is for | |---|---|---| | 22 | unused | free in the default mode | | 31 | identity hash | the value `System.identityHashCode` returns | | 4 | unused gap | reserved for Project Valhalla | | 4 | age | how many young collections the object has survived | | 1 | self-forwarded | a flag the collectors use while moving objects | | 2 | lock | the object's lock state | Some points interviewers probe: - **The hash is lazy.** A fresh object's hash bits are zero, meaning no hash yet. The JVM generates the identity hash the first time it is requested, through `System.identityHashCode` or an `Object.hashCode()` that the class does not override, and stores it in the header so the value stays stable for the object's life. - **An overridden `hashCode()` never touches the header.** Only the identity hash is stored; a value computed by a class's own `hashCode()` is not cached there. - **The age is a small counter.** It is how the generational collectors decide when to promote an object; how the threshold is chosen is a garbage-collection topic. - **Nothing for biased locking.** Older articles show a thread ID and an epoch in the mark word. Those belonged to biased locking, which was removed in JDK 18; the JDK 25 mark word has no such bits. ## The lock bits on JDK 25 The two lock bits, together with the rest of the word, describe the lock state. With the default **lightweight locking**: | Lock bits | State | Rest of the mark word | |---|---|---| | `01` | unlocked | the normal header: hash, age | | `00` | thin-locked | still the normal header; the owner is recorded on its own thread's lock stack | | `10` | inflated monitor | by default, a pointer to the `ObjectMonitor`, with the original header kept inside it | | `11` | marked | used by the garbage collector | The important change from older JVMs is the `00` row. Under the old stack-locking scheme, still selectable but deprecated on JDK 25, a thin lock replaced the mark word with a pointer to a lock record on the thread's stack and copied the header there. Lightweight locking leaves the header where it is and flips only the lock bits. In this default mode the header is moved out only when a lock inflates to a full monitor, and the identity hash survives that because the monitor keeps the original header. ## The class word The class word points at the class's **metadata structure** in metaspace, not at the `java.lang.Class` object on the heap. From it the JVM learns the object's type for casts and virtual calls, its instance size, and the offsets of its reference fields, which the garbage collector needs to trace the object. It is 4 bytes because compressed class pointers are on by default; what that compressed value encodes is a metaspace topic. ## Arrays: the length word An array is an object with one more header field: a 4-byte `int` **length** placed right after the class word. Its elements follow. 1. `new int[0]`: 8 + 4 + 4 = 16 bytes, already a multiple of 8. 2. `new int[1]`: 16 + 4 = 20 bytes, rounded up to 24. 3. `new long[0]`: also 16 bytes; `long` and `double` elements need 8-byte alignment, and offset 16 already provides it. 4. `new Object[2]` with compressed references: 16 + 2 × 4 = 24 bytes. ## The same header with compact object headers With `-XX:+UseCompactObjectHeaders` (a product option on JDK 25, off by default there, and the default from JDK 27 under JEP 534) the 22 unused bits at the top of the mark word hold a narrow class pointer, and the separate class word disappears. The header becomes 8 bytes, an array's length moves to offset 8, and inflated monitors are tracked in a side table so the mark word, which now carries the class, is never swapped out.

  • What happens to an object's identity hash when its lock inflates to a full monitor on a default JDK 25 JVM?
    The mark word is replaced by a pointer to the `ObjectMonitor`, with lock bits `10`, and the original header, hash and age included, is kept inside the monitor. `System.identityHashCode` reads it from there, so the value does not change. With compact headers on, the JVM tracks monitors in a side table instead and leaves the header in place.
  • Where do the elements of a long[] start compared with an int[] on a default 64-bit JDK 25 JVM?
    Both start at offset 16, after the 8-byte mark word, the 4-byte class word and the 4-byte length; 16 is already 8-byte aligned, so `long` elements need no gap. Under compact headers the array header is 12 bytes, so `int` elements start at 12 while `long` and `double` elements are pushed to 16 to keep their alignment.
  • Why do older descriptions of the mark word show a thread ID and an epoch that JDK 25 does not have?
    Those fields belonged to biased locking, which let one thread re-enter a lock without atomic instructions by stamping its ID into the header. Biased locking was removed in JDK 18. Uncontended locking on JDK 25 uses lightweight locking, which flips the lock bits to `00` and records the owner on the thread's own lock stack.

  • The JDK 25 mark word still holds a biased-locking thread ID and epoch.
  • Every object's identity hash is computed when the object is allocated.
  • On a default JDK 25 JVM a thin lock copies the mark word onto the thread's stack.
  • The class word points to the object's java.lang.Class instance on the heap.
  • A class's overridden hashCode() result is cached in the object header.
  • An array keeps its length inside the mark word.

Assumes JDK 25 defaults: lightweight locking, no biased locking (removed in JDK 18) and compact object headers off; compact headers change the mark word layout.