00:00

Compact Object Headers in Java 25

Last updated: · Product feature in Java 25 (opt-in)

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

Command
java -XX:+UseCompactObjectHeaders -jar app.jar
VersionJEPStatus
Java 24JEP 450experimental: needed -XX:+UnlockExperimentalVMOptions
Java 25JEP 519product 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.

HeaderSize.java
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");
    }
}
Output
UseCompactObjectHeaders = false
Heap used by 2000000 objects: about 39 MB

Compile and run: java HeaderSize

Command: java -XX:+UseCompactObjectHeaders HeaderSize

Output
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

Old and compact object header layouts A classic header uses an 8-byte mark word plus a 4-byte compressed class pointer, 12 bytes in total. A compact header packs a smaller class pointer into the 8-byte mark word. CLASSIC mark word 8 bytes class pointer 4 bytes header = 12 bytes empty object: 16 COMPACT mark word + class pointer packed into 8 bytes header = 8 bytes empty object: 8
Figure 1: The class pointer moves into the mark word, saving 4 bytes per object.

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

7. References