JDK 27 Turns On Compact Object Headers: Which Objects Shrink and Which Do Not

JDK 27 reached general availability on 2026-09-15. Two of its nine JEPs change a default instead of adding a feature, so they apply to every application that upgrades without touching a flag:

  • JEP 534 makes compact object headers the default object header layout.
  • JEP 523 makes G1 the default garbage collector in all environments.

This article measures the first change in detail and reports the second one briefly. The main question is practical: which objects in a heap actually get smaller.

Two terms

  • Object header. Every Java object starts with a few bytes of bookkeeping that the JVM uses for the object's class, locking, and garbage collection. Your fields come after it. JEP 534 describes the change as going "from 96 bits down to 64 bits", that is, from 12 bytes to 8.
  • Alignment. HotSpot rounds every object's size up to a multiple of 8 bytes by default. This is why a 4-byte smaller header does not always make an object smaller.

Together they give one rule that explains every number below: the header shrinks by 4 bytes, and alignment turns that into a saving of either 8 bytes or nothing.

Setup

MachinemacOS, arm64, 11 CPUs
JDK 21Corretto 21.0.6+7
JDK 25Temurin 25.0.5+8
JDK 27Temurin 27.0.1.0.2+1

All numbers come from the lab kept with this article's source (examples/jdk27-default-changes): three small Java programs and a script, JDK21=... JDK25=... JDK27=... ./run.sh. The heaps are synthetic. Nothing here was measured on a real service.

The defaults, read from the JVM

java -XX:+PrintFlagsFinal -version, with and without -XX:ActiveProcessorCount=1 -Xmx512m:

JDK 21, machine defaults           gc=UseG1GC       UseCompactObjectHeaders=absent
JDK 21, 1 CPU, -Xmx512m            gc=UseSerialGC   UseCompactObjectHeaders=absent
JDK 25, machine defaults           gc=UseG1GC       UseCompactObjectHeaders=false
JDK 25, 1 CPU, -Xmx512m            gc=UseSerialGC   UseCompactObjectHeaders=false
JDK 27, machine defaults           gc=UseG1GC       UseCompactObjectHeaders=true
JDK 27, 1 CPU, -Xmx512m            gc=UseG1GC       UseCompactObjectHeaders=true
  • JDK 25 has the compact header flag and it is off. JEP 519 made it a product feature in JDK 25 and said that making it the default was not a goal of that release. JDK 21 does not have the flag.
  • With one CPU, JDK 21 and 25 select the Serial collector and JDK 27 selects G1. It is the CPU count that triggers this in the lab, not the -Xmx512m. JEP 523 says older JDKs also chose Serial on machines with less than 1792 MB of physical memory; the lab did not test that case.

Bytes per instance

HeaderLab.java allocates 2,000,000 each of four small shapes and 1,000,000 customer records, keeps them alive, and waits. The script then runs jcmd <pid> GC.class_histogram, which prints the number of instances and the bytes used per class. Note that this command triggers a full garbage collection, so use it with care on a live service.

record Point(int x, int y) {}
final class OrderLine { final long id; final int qty; final int price; }
final class Address  { final String city; final String zip; }
final class Customer { final String name; final String email; final int age; final Address address; }
ClassJDK 25JDK 25 +UseCompactObjectHeadersJDK 27JDK 27 -UseCompactObjectHeaders
Point24161624
OrderLine32242432
Address24161624
Customer32242432
java.lang.Long24161624
java.lang.Integer16161616
java.lang.String (the object, without its characters)24242424
Used heap after a forced GC482 MiB420 MiB420 MiB482 MiB

JDK 21 produced the same sizes and the same 482 MiB as JDK 25 with its default.

The saving belongs to the flag, not to the version. JDK 25 with -XX:+UseCompactObjectHeaders gives the same sizes and the same 420 MiB as JDK 27. JDK 27 with the flag turned off goes back to 482 MiB.

Integer and the String object do not change. The rule from the top explains it:

ClassField bytesWith a 12-byte headerWith an 8-byte header
Integer41612, rounded up to 16
Long820, rounded up to 2416
Point820, rounded up to 2416
String1022, rounded up to 2418, rounded up to 24

The 10 bytes of String fields (a 4-byte array reference, a 4-byte hash, and two 1-byte fields) are read from the class source, not printed by the lab. The lab shows only that the measured size is 24 in both layouts.

Strings: it depends on the length

A String is two objects: the String itself and a byte[] with its characters. The array has a header too, so the same rule applies to it, and whether it saves 8 bytes depends on how many characters it holds.

StringLab.java allocates 1,000,000 distinct ASCII strings of one length, forces a GC, and divides the growth in used heap by the count. The result is bytes per string, for the String object and its byte[] together:

LengthJDK 27 -UseCompactObjectHeadersJDK 27 defaultSaved
448.040.08
848.048.00
956.048.08
1056.048.08
1256.048.08
1356.056.00
1556.056.00
1656.056.00
2064.056.08
2264.064.00
2464.064.00
2872.064.08
3680.072.08

JDK 25 with its default matched the flag-off column within 0.2 bytes.

Seven of the thirteen lengths save 8 bytes and six save nothing. A 36-character string, the length of a UUID in text form, goes from 80 to 72 bytes. A 16-character string stays at 56.

This matters for reading the first lab. Its customer records use names, e-mail addresses, city names and ZIP codes whose lengths happen to fall mostly on the "saves nothing" side, so the string-heavy part of that heap shrank by only 5.9% (273,410,080 to 257,248,792 bytes for Customer, Address, String and byte[] together), while the part made of Point, OrderLine, Integer and Long shrank by 25.0% (192,010,448 to 144,008,400 bytes). With different string lengths the first figure would be different. The 482 to 420 MiB total is a property of this synthetic mix, not a prediction for any real heap.

The collector change, briefly

On JDK 27 a JVM limited to one CPU now gets G1 instead of Serial. Serial stops the application and collects on a single thread. G1 does part of its work concurrently and in smaller steps, which is why it is the usual choice for services.

The lab ran a single-threaded allocation loop under -XX:ActiveProcessorCount=1 -Xms512m -Xmx512m, three times per configuration:

JDK 27, one CPUPausesAverage pausePeak RSS
Default (G1)81 to 9435 to 39 ms562 to 565 MiB
-XX:+UseSerialGC3388 to 96 ms539 to 550 MiB

RSS is the memory the operating system counts for the process, which is closer to what a container memory limit sees than the Java heap size. The average pause is the total pause time divided by the number of pause lines in the -Xlog:gc output; for G1 those lines include its short remark and cleanup pauses.

Read this table narrowly:

  • G1 paused more often and each pause was shorter on average.
  • G1 used 12 to 26 MiB more RSS than Serial with the same 512 MB heap. The Serial runs alone spread over 11 MiB, so the gap is small against the noise.
  • This is not a throughput comparison. ActiveProcessorCount=1 tells the JVM to size itself for one CPU, but on an 11-core laptop G1's background threads can still run on other cores. Under a real one-CPU limit they would compete with the application. The loop also spends about half of its time in garbage collection, which no service should.

The reliable finding is the first one in this article's flag table: the default collector for one-CPU environments changed.

Suggested checks

  • On JDK 25: try -XX:+UseCompactObjectHeaders in a staging environment and compare jcmd <pid> GC.class_histogram before and after. The lab shows the flag gives JDK 25 the same object sizes as JDK 27.
  • Before estimating a saving: look at the top classes in your own histogram. Small objects with a few numeric or reference fields save 8 bytes each. Integer saves nothing. Strings depend on their length.
  • Small containers moving to JDK 27: find out which collector each service runs today (-Xlog:gc prints it at startup). If it is Serial because of a one-CPU or low-memory environment, it will become G1 unless -XX:+UseSerialGC is set. Test with the real workload and the real limits before deciding.

Not tested

  • Linux, x86-64, and containers with real cgroup CPU and memory limits.
  • The low-memory condition (less than 1792 MB) that also selected Serial on older JDKs.
  • Any real service heap, strings with characters outside Latin-1 (which use two bytes per character), and arrays other than byte[].
  • Latency or throughput under a request workload, large heaps, startup time, and other collectors.
  • A Temurin build of JDK 21.

How this article was made

An AI agent chose the topic from the JDK 27 feature list, wrote the lab programs and the script, ran them, and wrote the text from the output. A second AI agent read the draft as a backend developer with three years of experience and pointed out that the first version drew a conclusion about strings from too few string lengths; the string-length measurement was added because of that review. A script checked that every number in the tables appears in the lab output or is computed from it. No person has reviewed the article or rerun the lab. The site owner approved publication.

Sources