Quick revision
The short answer
Compact object headers shrink each object's header from 12 to 8 bytes, so they help most in heaps full of small objects, but the gains vary by workload and the feature has less field experience because it is off by default. We decide by measuring heap after GC, GC CPU and throughput on representative services, checking that our agents, profilers and heap analysis tools work with it, rolling out through canaries on memory-bound services first, keeping a one-line switch in the base image to turn it off, and watching future JDK releases, which may change the default.
What to remember
In this answer, we saw that compact object headers cut the header from 12 to 8 bytes in JDK 25, helping heaps full of small objects most, with gains that vary by workload. The decision rests on measuring heap after GC, GC CPU and throughput on representative services, and on checking agents, profilers and heap tools. The rollout goes through canaries in memory-bound services, then service class by service class, with a base image switch to turn it off and a review on each new JDK release.
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.
Compact object headers shrink each object's header from 12 to 8 bytes, so they help most in heaps full of small objects, but the gains vary by workload and the feature has less field experience because it is off by default. We decide by measuring heap after GC, GC CPU and throughput on representative services, checking that our agents, profilers and heap analysis tools work with it, rolling out through canaries on memory-bound services first, keeping a one-line switch in the base image to turn it off, and watching future JDK releases, which may change the default.
Why this question is asked
JDK 25 is the first LTS where compact object headers are a supported product feature, and they promise something rare: a meaningful memory and CPU saving without changing a line of application code. A platform team responsible for hundreds of services has to decide whether to switch it on for everyone. Interviewers ask this as a scenario because the technical facts are only half the answer; the other half is how to adopt a new runtime feature safely at scale. In this answer, we will recap what the feature changes, look at where the gains come from and why they vary, list the compatibility checks, plan the rollout and keep a way back.
1A Quick Recap of What Changes
Every object on 64-bit HotSpot starts with a header. By default it is 12 bytes: an 8-byte mark word for the hash code, GC age and lock state, followed by a 4-byte compressed class pointer. Compact object headers, from Project Lilliput, squeeze a shorter class pointer into the mark word itself, so the header is 8 bytes in total. In JDK 24 the feature was experimental (JEP 450); in JDK 25 it became a product feature (JEP 519), still off by default and enabled with one flag.
Because the class pointer has fewer bits, the number of classes a JVM can load is limited, to a few million, far above what normal services use. That is worth knowing for unusual workloads that generate huge numbers of classes.
2Where the Gains Come From and Why They Vary
The saving is 4 bytes per object, but objects are padded to multiples of 8, so the saving only appears when it moves an object across an 8-byte boundary. An object with two int fields shrinks from 24 to 16 bytes, a third smaller. An Integer stays at 16 bytes. So the effect depends on the sizes of the objects a service actually holds.
Heaps dominated by many small objects, such as maps of small entries, ORM entity graphs and parsed JSON trees, gain the most, and published benchmark results have shown double-digit reductions in heap use and some CPU savings from fewer cache misses and less GC work. Heaps dominated by large arrays, such as byte buffers, gain almost nothing, because one header on a 1 MB array does not matter. So we cannot take any published figure as our number; we have to measure our own services.
| Heap content | Expected effect | Example |
|---|---|---|
| Many small objects | Large: heap after GC down, GC CPU down | Caches of small entries, entity graphs |
| Mixed objects | Moderate | Typical REST service |
| Large arrays and buffers | Little or none | Byte-stream proxies, image processing |
| CPU-bound with small heap | Small CPU change only | Calculation services |
3Measuring on Representative Services
We pick a handful of services that represent the fleet: one or two memory-bound ones with many small objects, a typical web service and one with mostly large buffers. For each, we run the same load test, or better, split production traffic between two canary instances, with and without the flag, for at least a few days. We compare heap after GC, the size of the live set, GC CPU time, allocation rate, throughput at the latency target and p99 latency.
Heap after GC is the most important number, because it is the live set, the memory the service really needs. If it drops by 15 percent, we may be able to lower the heap percentage or the container limit, which is where the cost saving comes from.
4Checking Compatibility
Off by default means fewer users have run it in production, so we check the tools around the JVM, not just the application. Java agents that instrument bytecode usually do not care about headers, but agents and profilers that read object layout or use the JVM's internal interfaces, such as some memory profilers, older async profilers or custom Unsafe-based code that computes field offsets, may assume a 12-byte header. Heap dumps in the standard HPROF format describe objects abstractly, so tools like Eclipse MAT normally keep working, but we confirm that our dump and analysis pipeline still produces correct sizes.
We also check flag combinations. Some JVM options or collectors may not support compact headers on every platform in a given release, so we read the JDK 25 release notes and confirm the JVM starts with our exact production options and logs no warning that the feature was disabled.
5Rolling Out and Keeping a Way Back
We roll out in rings. First canaries in the memory-bound services where the gain is largest and most visible. Then the rest of those services, then service class by service class, with a pause between rings to watch error rates, GC behaviour and any tool problems.
The switch lives in the shared base image as a variable, so turning it off is a configuration change and a restart, not a rebuild. Each ring keeps it on only if the numbers are at least neutral. Finally, we track future JDK releases: the default may change, and if it does, our rollout becomes a confirmation rather than an opt-in.
- Measure representative servicesHeap after GC, GC CPU, throughput, p99
- Tools and agents work?Profilers, APM agents, heap dump pipelineYesContinueNoFix or replace the tool first
- Canary memory-bound servicesLargest expected gain
- Numbers neutral or better?Compare with control instancesYesExpand to the next service classNoTurn off with the base image switch
- Review on each JDK releaseThe default may change
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Would you enable it for the whole fleet at once if the benchmarks look good?
No. Even with good numbers on a few services, a fleet-wide switch makes every service a test case at the same moment, and a problem with a profiler or agent would hit all of them. Rings let us learn cheaply, and the base image switch keeps each step reversible.
How do we turn the savings into cost reductions?
By lowering memory where the live set dropped. If heap after GC falls by 15 percent and the heap was sized with headroom around the live set, we can lower the container limit or pack more pods per node. Without that follow-up, the saving only shows up as fewer GCs.
Does it change application behaviour?
Not for ordinary Java code; object layout is invisible to the language. Code that depends on exact object sizes, such as hand-written memory estimators or Unsafe offset arithmetic, is the exception and must be tested.
7Conclusion
In this answer, we saw that compact object headers cut the header from 12 to 8 bytes in JDK 25, helping heaps full of small objects most, with gains that vary by workload. The decision rests on measuring heap after GC, GC CPU and throughput on representative services, and on checking agents, profilers and heap tools. The rollout goes through canaries in memory-bound services, then service class by service class, with a base image switch to turn it off and a review on each new JDK release.
Open with
I would treat it as a measured opt-in, not a fleet-wide flip. Compact object headers shrink the header from 12 to 8 bytes by folding the class pointer into the mark word, so services whose heaps hold many small objects, like caches, entity graphs or JSON trees, gain the most, while services full of large arrays gain little.
Then, if they want more
Published results are encouraging, but they vary by workload, so I would measure our own services: a few representative ones, with and without the flag, comparing heap after GC, GC CPU, allocation rate, throughput and p99.
Because it is off by default, there is less field experience, so I would check our tooling too: APM agents, profilers, anything that reads object layout or uses Unsafe offsets, and our heap dump analysis. I would also confirm the JVM starts with our exact production flags and does not silently disable the feature.
Then I would roll out in rings: canaries on memory-bound services first, then the rest of them, then service class by service class, keeping it only where the numbers are neutral or better. The switch would live in the base image as a variable, so turning it off is a config change and a restart.
And to actually save money, I would follow up by lowering limits where the live set dropped. Finally, I would watch future JDK releases, since the default may change.
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 "JDK 25 offers compact object headers as a product feature that is off by default. Your...": https://scrivio-interview-library.netlify.app/java/memory-layout/092c683700/