What do HotSpot's compact object headers change in an object's header layout, and what does the JVM give up to fit it into 64 bits?
answer
- one word instead of twelve bytes
- class pointer moves into the mark word
- 22-bit class pointer
- monitors move to a side table
- opt-in on 25, default on 27
basics
~20 sCompact object headers move the class pointer into the mark word, so the header is one 8-byte word instead of 12 bytes. The class pointer shrinks to 22 bits, bounding loadable classes near four million, and inflated monitors move to a side table.
solid answer
~50 sIn the default JDK 25 layout an object has an 8-byte mark word plus a 4-byte compressed class word. **Compact object headers** put a 22-bit class pointer into the mark word's 22 previously unused top bits, beside the unchanged 31-bit hash, 4 age bits, self-forwarded bit and 2 lock bits, so the header is a single 8-byte word: `new Object()` drops from 16 to 8 bytes and fields start at offset 8. The trades: 22 bits of class pointer instead of 32 bound the number of classes to roughly four million; compressed class pointers become mandatory; and because the mark word now carries the class it can no longer be swapped for a monitor pointer, so the JVM turns on its object monitor table. Experimental in JDK 24 (JEP 450), it is an opt-in product option in JDK 25 (`-XX:+UseCompactObjectHeaders`, JEP 519); JEP 534 makes it the default in JDK 27.
code
bash · 1 linejava -XX:+UseCompactObjectHeaders -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep -E 'UseCompactObjectHeaders|UseCompressedClassPointers|UseObjectMonitorTable'go deeper
Know that recent JDKs can shrink the object header to one 8-byte word, and that this makes small objects cheaper.
Describe the layout change: the class pointer moves into the mark word's unused top bits, the separate class word disappears, and new Object() becomes 8 bytes.
Explain the costs, a 22-bit class pointer, mandatory compressed class pointers, the monitor table, and predict which objects shrink and which stay the same size.
Decide whether to opt in on JDK 25 or plan for the JDK 27 default, based on measured heap and latency effects across the fleet rather than headline percentages.
## The default header and its slack On a 64-bit HotSpot JVM running JDK 25 with default settings, every object starts with a 12-byte header: - an 8-byte **mark word** holding the identity hash, GC age, a self-forwarded bit and the lock bits; - a 4-byte compressed **class word** pointing at the class's metadata. The mark word is not full. Its top 22 bits are unused in this mode, and 4 more bits below the hash are reserved for Project Valhalla. Meanwhile the class word costs 4 bytes in every object. **Compact object headers** use that slack to hold the class pointer, so the separate class word disappears. ## What moves where | Bits (most significant first) | Default JDK 25 mark word | Compact header | |---|---|---| | 22 | unused | narrow class pointer | | 31 | identity hash | identity hash | | 4 | reserved gap | reserved gap | | 4 | GC age | GC age | | 1 | self-forwarded | self-forwarded | | 2 | lock bits | lock bits | | separate class word | 4 bytes | none | The identity hash, age and lock bits are untouched; nothing was squeezed out of them. What changed is that the class pointer is now 22 bits and lives inside the mark word. The consequences for layout: - The header is **8 bytes**, so `new Object()` takes 8 bytes instead of 16. - Instance fields start at offset 8. - An array's 4-byte length sits at offset 8, so `int` elements start at offset 12, while `long` and `double` elements are aligned to 16. ## How much it saves The 4 saved bytes show up only when they move an object across an 8-byte boundary: | Object | Default JDK 25 | Compact headers | |---|---|---| | `new Object()` | 16 bytes | 8 bytes | | a class with one `int` field | 16 bytes | 16 bytes | | a class with one `long` field | 24 bytes | 16 bytes | | `long` + `int` + reference (compressed) | 32 bytes | 24 bytes | | `new int[1]` | 24 bytes | 16 bytes | The overall effect on a heap depends on its mix of object sizes. Heaps dominated by small objects gain the most; heaps dominated by large arrays gain almost nothing, because one header per large array is already negligible. ## What it gives up 1. **Fewer encodable classes.** A 22-bit class pointer has about four million distinct values, against 32 bits in the default mode. How a compressed class pointer maps to an address is a metaspace topic; the practical point is a hard ceiling on the number of loaded classes, far above what ordinary applications load. 2. **Compressed class pointers are mandatory.** If a JDK 25 JVM is started with `-XX:+UseCompactObjectHeaders` and `-XX:-UseCompressedClassPointers`, it warns that compact object headers require compressed class pointers and disables compact headers. 3. **Monitors move to a table.** In the default mode, inflating a lock replaces the mark word with a pointer to the `ObjectMonitor` and parks the original header inside it. With compact headers the mark word holds the class, which the JVM must be able to read at any time, so the header can never be swapped out. The JVM therefore turns on `UseObjectMonitorTable` and finds inflated monitors through a side table. 4. **Lightweight locking only.** If another locking mode was selected, compact headers switch the JVM back to the default lightweight locking; the deprecated legacy stack-locking also swapped the header out, which compact headers cannot allow. 5. **Offset assumptions break.** Tools or libraries that hard-code a 12-byte header or a 16-byte array base offset, instead of asking the JVM for field and array offsets, read the wrong bytes. ## Release status | Release | Status | How to enable | |---|---|---| | JDK 24 | experimental (JEP 450) | `-XX:+UnlockExperimentalVMOptions -XX:+UseCompactObjectHeaders` | | JDK 25 | product option, off by default (JEP 519) | `-XX:+UseCompactObjectHeaders` | | JDK 27 | default layout (JEP 534) | on; opt out with `-XX:-UseCompactObjectHeaders` | ## Weighing it on JDK 25 On JDK 25 turning compact headers on is an explicit, JVM-wide choice. The case for it is fewer bytes per small object, which means more objects per cache line and a smaller live set for the collector to trace. The case for caution is that it changes every object at once, and the monitor table changes how contended locks are found. Measure the heap and latency of a real workload with and without it before standardising, and expect the question to become an opt-out decision once services move to JDK 27.
- Why does HotSpot switch on its object monitor table when compact object headers are enabled?In the default JDK 25 mode, inflating a lock replaces the mark word with a pointer to the `ObjectMonitor` and keeps the original header inside it. Under compact headers the mark word holds the class pointer, which the JVM must be able to read at any moment, so it cannot be swapped out. The JVM therefore sets `UseObjectMonitorTable` and tracks inflated monitors in a side table.
- Why does a class with a single int field gain nothing from compact object headers?By default it is a 12-byte header plus a 4-byte `int`, exactly 16 bytes. With compact headers it is 8 + 4 = 12 bytes, which rounds up to 16. The 4 saved bytes disappear into alignment padding; savings appear only when the object's unpadded size crosses an 8-byte boundary.
- What does a JDK 25 JVM do when started with -XX:+UseCompactObjectHeaders and -XX:-UseCompressedClassPointers?It prints a warning that compact object headers require compressed class pointers and disables compact object headers, so the JVM runs with the ordinary header layout for that flag combination.
- Compact object headers are on by default in JDK 25.
- Compact headers make room by shrinking or dropping the identity hash.
- Every object shrinks by exactly 4 bytes with compact headers.
- Compact headers can be combined with -XX:-UseCompressedClassPointers.
- Compact headers still let a monitor pointer replace the mark word.
Assumes JDK 25, where -XX:+UseCompactObjectHeaders is a product option that is off by default; it was experimental in JDK 24 and becomes the default in JDK 27.