Quick revision
The short answer
Every Java object starts with a header: a mark word that holds the identity hash code, locking state and GC information, and a pointer to the object's class. On 64-bit HotSpot that header is normally 12 bytes, and compact object headers (JEP 519, a product feature in Java 25) squeeze it into 8, which saves heap for programs with many small objects. It is off by default and enabled with -XX:+UseCompactObjectHeaders, so before turning it on we measure heap, GC and throughput on realistic load and check that our agents and monitoring work with it.
What to remember
In this answer, we saw that every object carries a header with a mark word and a class pointer, normally 12 bytes on 64-bit HotSpot, and that Java 25's compact object headers fit both into 8 bytes, saving memory for workloads with many small objects. We also saw that the flag is off by default, and that we should measure heap, GC and throughput under real load and check our agents and tools before rolling it out through a canary.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Every Java object starts with a header: a mark word that holds the identity hash code, locking state and GC information, and a pointer to the object's class. On 64-bit HotSpot that header is normally 12 bytes, and compact object headers (JEP 519, a product feature in Java 25) squeeze it into 8, which saves heap for programs with many small objects. It is off by default and enabled with -XX:+UseCompactObjectHeaders, so before turning it on we measure heap, GC and throughput on realistic load and check that our agents and monitoring work with it.
Why this question is asked
Most of the heap in a typical Java service is not big arrays but millions of small objects: strings, boxed numbers, map entries, records and DTOs. For those objects, the header can be a third or more of their size. Shrinking the header by four bytes sounds tiny, but across millions of objects it can save a noticeable share of memory and GC work. Interviewers ask about this to see whether we understand how objects are laid out in memory and whether we approach a new JVM flag carefully. In this answer, we will recap what an object header holds, see how compact headers shrink it, work through a few object sizes, and then go through what we would check before enabling it.
1A Quick Recap of the Object Header
An object in the HotSpot heap is a block of memory with two parts: a header that the JVM uses for its own bookkeeping, followed by the object's fields. The header itself has two pieces.
The mark word is 64 bits on a 64-bit JVM. It holds the identity hash code once someone calls System.identityHashCode or the default Object.hashCode(), the locking state when a thread synchronizes on the object, and information the garbage collector uses, such as the object's age for promotion from the young generation. The class pointer, sometimes called the klass pointer, says which class the object is an instance of, so the JVM can find its methods and field layout. With compressed class pointers, which are the default, it takes 32 bits. So the classic header is 8 + 4 = 12 bytes, and objects are aligned to 8 bytes, so every object takes a multiple of 8 bytes in total.
2How Compact Headers Shrink It
Compact object headers merge the class pointer into the mark word. The class pointer is compressed further, to fewer bits, and the other information in the mark word is rearranged so that everything fits into a single 64-bit word. The header becomes 8 bytes instead of 12.
The feature was experimental in JDK 24 (JEP 450), where it needed -XX:+UnlockExperimentalVMOptions as well. In Java 25, JEP 519 made it a product feature, so the single flag -XX:+UseCompactObjectHeaders is enough, but it is still not on by default. Making the class pointer smaller limits how many classes the JVM can address, though the limit is far above what normal applications load, and some internal details of locking and hashing had to change to make room; check the JEP for the exact limits.
| Object | Classic header (12 bytes) | Compact header (8 bytes) |
|---|---|---|
new Object() | 12 -> 16 bytes | 8 bytes |
| Integer (one int) | 12 + 4 = 16 bytes | 8 + 4 = 12 -> 16 bytes |
| Long (one long) | 12 + 8 = 20 -> 24 bytes | 8 + 8 = 16 bytes |
record Point(int x, int y) | 12 + 8 = 20 -> 24 bytes | 8 + 8 = 16 bytes |
| String object (without its array) | 12 + 10 = 22 -> 24 bytes | 8 + 10 = 18 -> 24 bytes, no saving |
3Measuring Layouts With JOL
We do not need to work these numbers out by hand. JOL, the Java Object Layout tool from the OpenJDK project, prints the real layout of an object in the JVM we are running.
Notice that Point drops from 24 to 16 bytes, a third smaller. A service holding ten million such objects saves about 80 MB of heap. For objects with large fields or for big arrays, the saving is small in relative terms, which is why the benefit depends so much on the workload. The JEP reports heap reductions of around 10 to 20 percent on some benchmarks, along with less GC work, but our own numbers are what count.
4What We Would Check Before Turning It On
First, we measure, because the gain varies. We run the service with and without the flag under the same realistic load, ideally a replay of production traffic, and compare live heap after full GC, allocation rate, GC frequency and pause times, CPU use and request latency. A canary in production, with a few instances using the flag beside unchanged ones, gives the most honest comparison. If we save memory, we decide whether to keep the same heap and gain GC headroom, or shrink containers and save cost.
Second, we check our tools. Anything that reads object memory directly can be affected: Java agents that instrument allocation, profilers and APM agents, heap dump analysers, and libraries that use sun.misc.Unsafe with hard-coded field offsets. We confirm that our APM agent's version supports the setting, that a heap dump opens correctly in the analyser we use, and that JFR, async-profiler or whatever we use for allocation profiling still works.
Third, we check the rest of the runtime setup. The flag should be set together with any CDS or AOT cache we use, because archives are created for a specific header layout, and the JDK may ship a separate default archive for compact headers; check your build. We also confirm which garbage collectors and platforms support the feature in our JDK version, and we keep the change easy to roll back, by setting it in the standard launch configuration rather than editing each service.
- Check agents, profilers and heap toolssupported versions, Unsafe-based libraries
- Load test with and without the flaglive heap, GC, CPU, latency
- Worth it?YesCanary a few production instancesNoKeep the default, record the result
- Canary healthy for a week?YesRoll out through the base launch configNoRoll back by removing the flag
4 steps in order. Play them, or step through them one at a time.
5Follow-up questions
Does compact headers change anything in our Java code?
No. Object headers are invisible to Java code, so the language, the APIs and the behaviour of hashCode, synchronized and instanceof stay the same. Only code that reads memory directly, through Unsafe offsets or native tools, can notice the different layout.
Why is it not on by default in Java 25?
Because it is a big change to a core part of the JVM, and the OpenJDK project usually gives such features time in production before making them the default. Some platforms or configurations may not support it, and tools need time to catch up. A later release may switch the default; check the release notes of the JDK we upgrade to.
How does this relate to compressed oops?
Compressed oops shrink references between objects, the fields that point to other objects, from 64 to 32 bits for heaps up to about 32 GB. Compact headers shrink the header at the start of every object. They are independent savings, and a typical small-object heap benefits from both.
6Conclusion
In this answer, we saw that every object carries a header with a mark word and a class pointer, normally 12 bytes on 64-bit HotSpot, and that Java 25's compact object headers fit both into 8 bytes, saving memory for workloads with many small objects. We also saw that the flag is off by default, and that we should measure heap, GC and throughput under real load and check our agents and tools before rolling it out through a canary.
Open with
Every object in HotSpot starts with a header. The first part is the mark word, 64 bits holding the identity hash code, the lock state and GC information like the object's age.
Then, if they want more
The second is the class pointer, which says what class the object is, and with compressed class pointers it is 32 bits, so the header is 12 bytes. Compact object headers fold the class pointer into the mark word, so the header is 8 bytes.
It was experimental in JDK 24 and became a product feature in Java 25 with JEP 519, enabled with -XX:+UseCompactObjectHeaders, but it is still off by default.
The gain depends on the workload: a record with two ints goes from 24 to 16 bytes because of 8-byte alignment, while an Integer stays at 16, so services with millions of small objects benefit most.
Before turning it on, I would measure: run the service with and without the flag under realistic load and compare live heap, GC frequency and pauses, CPU and latency, then canary it in production.
I would also check tooling, meaning our APM agent version, profilers, heap dump analysers and any library using Unsafe offsets, and make sure our CDS or AOT archives are built with the same setting. I would roll it out through the standard launch configuration so it is easy to revert.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java 25 made compact object headers a product feature (JEP 519). What are object...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/f56f04c2f8/