Java heap explained: young and old generations, G1 regions, and what -Xmx really caps

Ask five Java developers what the heap is and you will get five pictures: a big box with a size limit, two boxes called young and old, a grid of regions, or just "the thing -Xmx sets". All of them are partly right. This guide puts the pieces together for the HotSpot JVM that ships with OpenJDK: how the heap is divided, why it is divided that way, what the size options control, and which memory they never touch.
What the heap holds, and what it does not
The heap is the area where the JVM allocates objects and arrays created with new, and the only area the garbage collector reclaims object by object. Everything reachable from a GC root stays; everything else is garbage, a point developed in GC roots and memory leaks.
A lot of what a Java process uses is not heap: thread stacks, class metadata in Metaspace, compiled code in the code cache, direct byte buffers, the collector's own bookkeeping, and memory allocated by native libraries. Keep that list in mind; it explains the most common surprise of all, a process whose resident memory is far larger than its -Xmx.
The generational idea: most objects die young
HotSpot's collectors are built on an observation called the weak generational hypothesis: most objects become unreachable soon after they are allocated, while the few that survive a while tend to live a long time. Request objects, temporary strings, iterators and boxed numbers vanish within milliseconds; caches, connection pools and configuration live as long as the application.
The heap is therefore split into two generations that are collected differently.
- The young generation is where new objects are allocated. It is made of eden, where allocation happens, and two survivor spaces. A young collection copies the live objects out of eden and the occupied survivor space into the other survivor space, and treats everything left behind as free. Because few objects survive, copying them is cheap, and the cost of a young collection depends on what is alive, not on how much was allocated.
- The old generation receives objects that have survived a number of young collections (their "age" passes the tenuring threshold) or that do not fit in a survivor space. It is collected less often and by different means, because copying a large, mostly live area every time would be expensive.
The proportion between the two is tunable. Oracle's HotSpot GC tuning guide gives the classic example: with -XX:NewRatio=3, the ratio between young and old is 1:3, so eden plus the survivor spaces take one fourth of the heap. The same chapter names the total heap size and the share of the heap given to the young generation as the two most important factors for collection performance.
G1: the same generations, cut into regions
Since JDK 9 the default collector on server-class machines is G1, and G1 does not keep eden, survivor and old as three contiguous blocks. It divides the whole heap into equal-sized regions, and each region plays a role for a while: eden, survivor, old, or free. Young collections evacuate eden and survivor regions; mixed collections also evacuate the old regions with the most garbage, which is where the name "garbage first" comes from.
The region size is chosen at startup. The G1 chapter of the tuning guide explains that the default is calculated from the maximum heap size to give roughly 2048 regions, with a maximum ergonomically determined value of 32 MB, and that a size set by hand with -XX:G1HeapRegionSize must be a power of 2 between 1 and 512 MB.
The region size matters because of humongous objects. According to the same chapter, an object larger than or equal to half a region is humongous: it is allocated directly in the old generation, in one or more contiguous regions of its own. A service that builds large byte arrays or big collections can fill its old generation with humongous regions without any long-lived data, a pattern worth checking in the GC log before raising the heap. The practical options for G1 are covered in tuning G1 in production.
ZGC, the low-latency collector, also uses regions (it calls them pages) of several sizes and, since JDK 23, runs in generational mode by default; the older single-generation mode has been removed. Shenandoah, Serial and Parallel keep their own layouts, but the young/old logic is the common thread.
What -Xmx and -Xms actually set
-Xmx sets the maximum size of the heap, and nothing else. The reference page of the java command describes it as the maximum size of the heap, equivalent to -XX:MaxHeapSize, and notes that for server deployments -Xms and -Xmx are often set to the same value. -Xms is the initial size: the heap starts there and the JVM grows or shrinks it between the two bounds as the application runs.
Without either option the JVM picks for you. On a server-class machine, the ergonomics chapter lists an initial heap of 1/64 of physical memory and a maximum heap of 1/4 of physical memory.
In a container the JVM reads the container's memory limit instead of the host's, and the percentage options take over. The java command reference gives the default value of -XX:MaxRAMPercentage as 25 percent.
That is why a JVM in a 2 GB pod gets about 512 MB of heap unless someone raises it. The sizing rules for pods are in the JVM in containers.
Two side effects are worth knowing. Setting -Xms equal to -Xmx avoids resizing pauses and makes the footprint predictable, at the price of reserving the full heap from the start. And going past roughly 32 GB switches off compressed oops, so a slightly larger heap can hold less data, as explained in compressed oops and the 32 GB boundary.
What -Xmx leaves out
A JVM started with a 4 GB heap routinely shows 5 or 6 GB of resident memory. The difference is not a leak; it is everything outside the heap:
| Area | Main option | Typical symptom when it runs out |
|---|---|---|
| Metaspace (class metadata) | -XX:MaxMetaspaceSize | OutOfMemoryError: Metaspace |
| Thread stacks | -Xss, number of threads | unable to create native thread, StackOverflowError |
| Code cache (JIT-compiled code) | -XX:ReservedCodeCacheSize | "CodeCache is full" warning, slower code |
| Direct buffers | -XX:MaxDirectMemorySize | OutOfMemoryError: Direct buffer memory |
| GC data structures, symbols, native libraries | (no single cap) | container killed by the OOM killer |
Native Memory Tracking breaks this bill down category by category; see why your JVM uses more memory than -Xmx. For the heap-only case, when the error really is Java heap space, the triage steps are in OutOfMemoryError: Java heap space.
Seeing the layout of a running JVM
You do not have to guess. Three commands, all shipped with the JDK, show how a live heap is organised:
# final heap settings chosen by ergonomics
java -XX:+PrintFlagsFinal -version | grep -E ' (MaxHeapSize|InitialHeapSize|G1HeapRegionSize|NewRatio) '
# current layout and occupancy of a running process
jcmd <pid> GC.heap_info
# one line per collection, with heap before and after
java -Xlog:gc -jar app.jar
With G1, GC.heap_info prints the region size and how many regions are currently young and survivor, and the unified GC log shows each pause with the heap occupancy before and after. Reading those lines over a day says more about the right -Xmx than any rule of thumb; the format is explained in reading GC logs.
Five things to remember
- The heap holds objects; stacks, classes, compiled code and direct buffers live elsewhere.
- Young collections are cheap because few objects survive; a growing old generation is what deserves attention.
- Under G1, objects of half a region or more are humongous and go straight to the old generation.
- Without options, the maximum heap is a quarter of the memory the JVM sees, container limits included.
- Size the container for heap plus everything else, never for
-Xmxalone.
Sources
- Oracle, HotSpot Virtual Machine Garbage Collection Tuning Guide, Java SE 25: Factors Affecting Garbage Collection Performance (generations, NewRatio), Garbage-First (G1) Garbage Collector (regions, humongous objects, region size), Ergonomics (default heap sizes)
- Oracle, The java Command, Java SE 25 (-Xmx, -Xms, MaxRAMPercentage)
- OpenJDK, JEP 474: ZGC: Generational Mode by Default (JDK 23) and JEP 490 (removal of the non-generational mode)
MemoryJava heap space
The most common OutOfMemoryError has three different causes. How to tell an undersized heap, a leak and one oversized request apart.
MemoryCompressed oops
Raise -Xmx from 31g to 32g and every reference doubles in size. How compressed oops work, how to see which mode you got, and when crossing the line is worth it.
Garbage collectionTuning G1
Heap size first, a realistic pause target, then IHOP and region size, and why -Xmn works against G1.