Memory

Compressed oops and the 32 GB heap boundary: why -Xmx32g can hold less than -Xmx31g

Memory · · Published 2026-11-16

A wall of old bronze post office boxes with four-digit numbers on white labels, arranged around a closed service window in a post office
A short box number is enough to find any box in the wall, as long as the wall stays small enough. Photo: Jonathan Desrosiers, WordPress Photo Directory, CC0.

A service runs with a 28 GB heap and needs a little more room, so someone bumps -Xmx to 32g. After the restart the heap fills faster than before, collections are more frequent, and the extra four gigabytes seem to have vanished. Nothing is broken: the JVM has quietly stopped using compressed oops, and every object reference in the heap has grown from four bytes to eight.

What an oop is, and why it is squeezed

In HotSpot vocabulary, an oop, short for "ordinary object pointer", is a managed reference to a Java object. On a 64-bit JVM it would naturally be a 64-bit address. The HotSpot wiki page on compressed oops explains the trade-off: a 32-bit JVM cannot address much more than 4 GB, while a plain 64-bit heap can need around one and a half times the memory for the same program, because every pointer has doubled. Memory is cheap, the page notes, but cache and memory bandwidth are not.

Compressed oops are the compromise. References stored in the heap are kept as 32-bit values. Because objects are aligned on 8-byte boundaries, the three low bits of every real address are always zero, so the JVM can store the address shifted right by three and shift it back when it needs it. Thirty-two bits times eight gives 32 GB of addressable heap, or about four billion objects.

According to the same page, three kinds of fields are compressed when the option is on:

Local variables, stack slots and the JVM's own C++ structures stay at full width. The saving is concentrated where it matters: in the heap, which is what the garbage collector has to scan and copy.

The four modes, and how to see yours

Compression is not a single setting. When the JVM starts, it tries to reserve the heap at an address that makes decoding as cheap as possible, and falls back step by step. The wiki describes the strategy, and the JVM reports its choice with unified logging:

java -Xmx8g -Xlog:gc+heap+coops=debug -version

On an OpenJDK 21 build for Linux x64 with the default G1 collector, our runs produced these lines (abridged):

-Xmx2g   Heap address: 0x0000000080000000, size: 2048 MB, Compressed Oops mode: 32-bit
-Xmx8g   Heap address: 0x0000000600000000, size: 8192 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
-Xmx31g  Heap address: 0x0000001001000000, size: 31744 MB, Compressed Oops mode: Non-zero disjoint base: 0x0000001000000000, Oop shift amount: 3

In everyday operation the difference between these modes is small compared with what happens when compression is switched off altogether. A quicker check, available on every JVM, is the final flag value:

java -Xmx32g -XX:+PrintFlagsFinal -version | grep -E 'UseCompressed(Oops|ClassPointers) '

On a running process, jcmd <pid> VM.flags shows the same information, and the gc,init log tag prints a line such as Compressed Oops: Enabled (Zero based) at startup if you already log GC with unified logging.

The cliff just below 32 GB

The 32 GB limit applies to the whole reserved heap, not only to -Xmx as typed. The JVM also needs a protected page and rounds the heap to its own alignment, so the real switch-off point sits slightly below 32 GB. On the same JDK 21 build with G1, -Xmx32736m still printed UseCompressedOops = true, while -Xmx32752m and above printed false. Treat these values as an illustration, not a rule: the exact edge depends on the collector, the region size and the platform. The safe habit is to stay at 31g or below, or to check the flag every time you move close to the limit.

What does crossing the line cost? Each reference field and each array slot doubles from 4 to 8 bytes. The object header is a separate matter: JEP 450 gives the classic 64-bit header as between 96 bits (12 bytes) and 128 bits (16 bytes) depending on configuration, the larger size applying when class pointers are not compressed. On recent JDKs compressed class pointers stay on above 32 GB (our 40g run still showed UseCompressedClassPointers = true), so it is mainly the references that grow. The overall effect depends on how pointer-heavy the data is. A heap made mostly of large byte[] buffers barely changes; a heap full of small objects, linked maps and collections of references can grow considerably. The only reliable number is your own: take a heap histogram (jcmd <pid> GC.class_histogram) of the same workload on both sides of the boundary and compare the live set.

If the live set grows by more than the memory you added, the bigger heap is a smaller one in disguise.

Options for going beyond 32 GB

  1. Stay below and scale out. Two JVMs with 30 GB each often behave better than one with 60 GB, for GC as well as for pointer size. This is the usual answer for stateless services.
  2. Raise the object alignment. -XX:ObjectAlignmentInBytes=16 makes the shift four bits instead of three, so 32-bit references cover 64 GB. In our test, -Xmx40g alone gave UseCompressedOops = false, and -Xmx40g -XX:ObjectAlignmentInBytes=16 gave true. The price is padding: every object is rounded up to a multiple of 16 bytes, which wastes memory on small objects. Measure the live set before and after; it is a niche option, not a default.
  3. Go well past the boundary. If the data really needs it, pick a heap size large enough that the doubling no longer matters relative to what you gain, and accept uncompressed references. A heap just above 32 GB is usually the worst of both worlds.

ZGC is a special case

ZGC stores metadata in its references (the "coloured pointers" that let it relocate objects concurrently), so it does not use compressed oops at all. On our JDK 21 build, -XX:+UseZGC -Xmx8g reported UseCompressedOops = false even with a small heap, while compressed class pointers stayed on. Two consequences follow. The 32 GB cliff does not exist with ZGC, because there is nothing to lose when crossing it. And moving a service from G1 to ZGC at the same -Xmx can increase its live set, a point worth adding to the headroom calculation for a ZGC heap and to the comparison in choosing a garbage collector.

Compact object headers change the arithmetic

Project Lilliput has been shrinking the other half of the per-object overhead. Compact object headers reduce the header to 64 bits (8 bytes) on x64 and AArch64. They arrived as an experimental option in JDK 24 (JEP 450), became a product feature in JDK 25 with JEP 519, enabled with -XX:+UseCompactObjectHeaders, and JEP 534, delivered in JDK 27, makes them the default header layout. JEP 534 summarises the change as headers going from 96 bits down to 64 bits.

Compact headers do not replace compressed oops: reference fields are still 4 or 8 bytes depending on UseCompressedOops. They make each object smaller, so the same data needs less heap, which can keep a service under the 32 GB line where it used to need more.

A short checklist

Sources