Compact Object Headers in Java 25
Every Java object starts with a header used by the JVM for locking, hashing, garbage collection and the object's class. Compact object headers shrink this header from 12 bytes to 8 bytes on 64-bit platforms. Experimental in Java 24 (JEP 450), they are a product feature in Java 25 (JEP 519), enabled with -XX:+UseCompactObjectHeaders.
This tutorial explains where the savings come from and measures them with a small program.
1. Why Do Object Headers Matter?
Many applications create huge numbers of small objects: map entries, boxed numbers, small records. For these objects the header is a large share of their size. Saving 4 bytes per object can reduce heap usage noticeably, improve CPU cache efficiency and lower garbage-collection work, without any code change.
2. Enabling the Feature
java -XX:+UseCompactObjectHeaders -jar app.jar
| Version | JEP | Status |
|---|---|---|
| Java 24 | JEP 450 | experimental: needed -XX:+UnlockExperimentalVMOptions |
| Java 25 | JEP 519 | product feature, still off by default |
3. Practical Example
Example: Measuring the heap used by 2 million objects
The program creates two million plain Object instances, the case where the header is the entire object, and measures the heap before and after. It is run twice on JDK 25.
import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.ManagementFactory;
public class HeaderSize {
public static void main(String[] args) {
HotSpotDiagnosticMXBean vm = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
System.out.println("UseCompactObjectHeaders = " + vm.getVMOption("UseCompactObjectHeaders").getValue());
Runtime rt = Runtime.getRuntime();
System.gc();
long before = rt.totalMemory() - rt.freeMemory();
// 2 million tiny objects: their size is dominated by the object header
Object[] objects = new Object[2_000_000];
for (int i = 0; i < objects.length; i++) {
objects[i] = new Object();
}
System.gc();
long after = rt.totalMemory() - rt.freeMemory();
System.out.println("Heap used by " + objects.length + " objects: about "
+ Math.round((after - before) / (1024.0 * 1024.0)) + " MB");
}
}
UseCompactObjectHeaders = false Heap used by 2000000 objects: about 39 MB
Compile and run: java HeaderSize
Command: java -XX:+UseCompactObjectHeaders HeaderSize
UseCompactObjectHeaders = true Heap used by 2000000 objects: about 24 MB
The array of 2 million references needs about 8 MB in both runs. Each object needs 16 bytes with the old 12-byte header (rounded up to 8-byte alignment) but only 8 bytes with a compact header, so the total drops from about 39 MB to about 24 MB. Real applications have larger objects, so typical savings are smaller but still significant.
4. What Changes in the Header
5. Important Points and Best Practices
- Measure with your own workload; the benefit depends on how many small objects you have.
- An AOT or CDS cache must be created with the same setting; the JVM reports the mismatch otherwise.
- The feature is off by default in Java 25; later releases plan to change that (JEP 534 targets Java 27).
- No code changes are needed; only the JVM option.
6. Summary
- Java 25 (JEP 519) made compact object headers a product feature.
- Headers shrink from 12 to 8 bytes on 64-bit JVMs.
- Enable them with
-XX:+UseCompactObjectHeaders; they are off by default. - Programs with many small objects use noticeably less heap.
Related Tutorials
- Garbage Collection in Java
- Generational Shenandoah
- Ahead-of-Time Command-Line Ergonomics
- Generational ZGC (Java 21)
7. References
Frequently Asked Questions (FAQ)
Q1. What are compact object headers?
A JVM feature that reduces the size of every object header from 12 to 8 bytes on 64-bit platforms, which lowers memory usage.
Q2. Are compact object headers enabled by default in Java 25?
No. In Java 25 they are a product feature that you enable with -XX:+UseCompactObjectHeaders.
Q3. Do I need to change my code?
No. It is a JVM option; application code is unaffected.
Q4. How much memory can I save?
It depends on object sizes. The saving is 4 bytes per object; programs with many small objects benefit the most.