Lesson · Garbage Collection & References
Heap layout: Eden, survivors and the old generation
15 min readHeap Layout
A generational heap is a bet that most objects die young. Eden, the two survivor spaces and the old generation are how HotSpot collects on that bet, and the card table is what keeps it affordable.
The bet: most objects die young
A collector that treats every object the same has to ask the same question about all of them: can the program still reach this one? Your code usually knows the answer already. The request body, the builder and the temporary it just created die before the next statement runs.
The generational heap turns that observation into a design. The young generation is collected cheaply and often, and whatever survives it is moved somewhere quieter and collected rarely and expensively. The bet is that objects which survived a while will keep surviving, so it is worth paying to separate them from the crowd that dies.
HotSpot splits that model into three young spaces plus the old generation. Eden holds every new object. Two survivor spaces, S0 and S1, hold objects that already survived one collection, and one of the two is always empty. The old generation holds whatever made it through the filter.
| Space | What lives there | How it is emptied |
|---|---|---|
| Eden | every new object, until the next minor GC | live objects are copied out, and the whole space is declared empty |
| Survivor (S0 and S1) | objects that survived at least one minor GC, each carrying an age | they are copied into the other survivor and the age goes up |
| Old generation | survivors that passed the tenuring threshold, plus oversized objects | a major or full GC marks and compacts them in place |
Collecting the young generation costs what survives, not what was allocated. A program that allocates a gigabyte of short-lived junk and keeps a megabyte pays for the megabyte.
Allocation is a pointer bump in a thread-local buffer
Allocating inside an empty region is the cheapest thing a machine can do: take the next address, move the pointer, hand out the space. HotSpot does exactly that, with one complication. Many threads allocate at once, and one shared bump pointer would turn every allocation into a lock.
The fix is the thread-local allocation buffer (TLAB). Each thread reserves a chunk of Eden for itself and bumps a private pointer inside it, so the fast path touches nothing shared. When the chunk runs out, the thread retires it and reserves a new one.
- TLAB, per thread: a private slice of Eden. Allocations inside it are a bump of a thread-local pointer, with no contention on the way.
- Retirement and waste: a retired chunk leaves a tail that may be too small for that thread's next object, and those leftovers go back to Eden's shared path. Eden therefore fills in pieces rather than on one clean frontier, and the JVM budgets the waste at roughly one percent of Eden.
- Adaptive sizing: the JVM watches allocation rates and grows or shrinks TLABs to match. An allocation-heavy thread gets bigger chunks; a quiet thread does not tie up Eden it will never use.
- Zeroing is included: fields must start at their default values, so the space is zeroed before an object can use it. Bump allocation is cheap, not free.
- Escape analysis sits on top: when the JIT proves that an object never escapes its method, it can delete the allocation entirely. That is the only way the bump gets cheaper.
Allocation is a pointer bump in a thread-local buffer, and it can be that cheap only because the space being bumped into is empty by construction. Making it empty again is the collector's job.
Minor GC: copy the living, ignore the dead
Eden runs out, and a minor collection starts. The collector walks from the GC roots, finds every live object in Eden and in the current from-survivor, and copies each one into the to-survivor, which is empty and waiting. An object with no path from a root is never visited at all: garbage costs nothing, not even a look.
The copy compacts as a side effect. Survivors land contiguously in the destination, so the space has no holes and needs no free list. Then the survivor spaces swap roles: the filled one becomes the from-space, the emptied one becomes the destination for the next cycle.
Copying cannot run while application threads mutate the graph, so they pause for it. That pause is proportional to what survives, which is why the design works: it is short exactly when the generational bet pays off.
- Walk the roots: live frames, static fields, and the cross-generation records the card table provides.
- Copy the live objects from Eden and the from-survivor into the empty to-survivor, updating every reference that pointed at them.
- Increment each copied object's age by one; the age lives in the object header.
- Declare Eden empty and swap the roles of the two survivor spaces.
One survivor space must stay empty: it is the budget the next copy spends. That is the whole reason there are two of them.
Aging out: promotion to old
The age an object carries is a survival count, and it decides when copying stops being worth it. Once the age reaches the tenuring threshold (15 by default, set with -XX:MaxTenuringThreshold), the next collection moves the object to the old generation instead of the other survivor space. Promotion is effectively one-way: young objects are promoted, old objects are not sent back.
Two shortcuts skip the queue. An object too large for the young spaces is allocated straight into old, because copying a big array between survivors would waste the space it is competing for. And when the destination survivor overflows, the collector promotes the overflow instead of failing, which is how objects with single-digit ages end up in old.
Under pressure the JVM also adapts: HotSpot lowers the effective tenuring threshold when survivor spaces are crowded, moving more objects to old to keep copy costs down. Nothing in your code declared those objects long-lived; the collection cadence did.
-XX:SurvivorRatio=8 // Eden is 8x one survivor: each survivor is 10% of young -XX:MaxTenuringThreshold=15 // copies before promotion; the JVM may lower it adaptively // Initial sizing only: adaptive collectors resize the spaces from there.
Age is the only signal the collector has. An object in old is not there because it is important, but because it survived long enough for copying to stop paying.
Heap layout · Eden → survivor → old
thread-local buffers
A fresh JVM: Eden, two survivor spaces and the old generation, all empty. New objects land in Eden, and S0 and S1 take turns holding survivors, never both at once.
Counts and buffer sizes are illustrative: a real Eden holds far more than five objects before it fills.
The old generation and the full collection
Old has no twin to copy into, so a major or full collection marks what is reachable and compacts it in place, sliding survivors together. That is slower per byte than evacuating a young space, and it touches most of the heap, which is why it runs far less often.
The asymmetry is a diagnostic tool. Minor collections are routine background noise; full collections are events. A full GC every few minutes under steady load means the heap holds data that lives too long (sessions, unbounded caches, static maps), or that objects are being promoted too young. Resizing the old generation changes when the full GCs happen, not whether they need to.
A full GC is a lifetime signal, not a tuning signal. When it runs often, look at what reaches old, not at the flags.
Cross-generation pointers and the card table
Copying young objects means updating every reference to them, including references that live in old. Finding those old-to-young pointers by scanning the entire old generation at every minor GC would give back everything the generations bought.
The design pays a smaller price instead. Every reference store goes through a write barrier, a few instructions the compiler inserts at the store, and the barrier records where the store happened. The record is the card table: one entry per 512-byte card of heap, marked dirty when a store into that card may have created an old-to-young pointer.
- Write barrier: runs at reference stores and marks the containing card dirty. The cost is paid on every such store, not only on collections.
- Card table: a coarse map of the heap, one byte per card, cleaned by the collector after it has checked the card.
- Minor GC: treats dirty cards as extra root sources. A young object found this way survives even when nothing young points at it.
- The trade: a small permanent cost per store in exchange for a young collection that never depends on the size of the old generation.
The write barrier is the price of not scanning the old generation. Generation separation is a bargain between allocation speed, barrier cost and pause time, not a free win.
Heap layout · generations
counts are illustrative, not to scale
each thread bumps its own reserved chunk; retired tails go back to the shared path
write barrier + card table
h.child = freshcard 3 dirtySame generations, different unit
Everything above is about logical spaces, not about contiguous addresses. G1, the default collector since Java 9, splits the heap into equal regions and gives each region a role: young, survivor or old, reassigned from collection to collection. Eden is a set of regions, the survivors are regions, and old is the rest, while bump allocation, copying, tenuring and card-based remembering all still run.
The unit changes; the model does not. Regions are also what lets a collector pick a handful of old regions worth evacuating instead of collecting all of them, and why a very large object gets special treatment. That is the next lesson (Collectors); the generational model is what survives the switch.
The four spaces are a model, not a memory map. Ask what a collector does with a young object and with an old object; the answers stay the same across region-based designs.
Reading the picture in production
The layout explains most young-generation symptoms before any tooling is involved:
- Minor GCs are frequent and cheap: the young generation is small for the allocation rate. Either size it up or reduce garbage; the collections themselves are not the problem.
- Minor GCs are frequent and expensive: the survivor set is large. Look for short-lived volume that survives a copy before touching any flag.
- Full GCs appear under steady load: something is reaching old that should have died. Unbounded caches, sessions and static collections are the usual suspects.
- Promotion happens early: either the survivor spaces are overflowing (bursty allocation) or the objects genuinely live long. Watch the ages in a GC log before deciding which.
- To see any of it, run with -Xlog:gc* and read the space sizes and causes the collector reports; everything else is a guess about a different JVM.
Young collections are about allocation rate; full collections are about object lifetimes. Two different questions, two different fixes.
A GC storm promotes your short-lived objects
Promotion is decided by age, and age climbs once per copy. A burst of allocations that triggers a run of minor collections marches objects that were never meant to last across the survivor spaces and into old, where only a full collection can remove them. This is premature promotion, and it is why enlarging the young generation sometimes lowers old-generation occupancy: the same objects now die inside Eden, before any copy happens.
Interview angles
What does a minor GC actually do?+
It walks the roots, copies the live objects from Eden and the from-survivor into the empty to-survivor, increments their ages, and then swaps the roles of the two survivor spaces.
Unreachable objects are never visited, so the cost is proportional to what survives. The copy also compacts the destination, which is why young collections leave no fragmentation behind.
Common misconception: That a minor GC scans the whole heap. It scans the young spaces plus the remembered set from old; the old generation itself is not walked.
Follow-up: What triggers promotion during that copy?
What is a TLAB and why does allocation need one?+
A thread-local allocation buffer is a private chunk of Eden that one thread bumps a pointer inside, so ordinary allocations take no lock and touch no shared state.
The JVM sizes TLABs adaptively. Wasted tails at retirement are budgeted (about one percent of Eden), and they are why Eden fills in pieces rather than on a single frontier.
Common misconception: That TLABs are a general-purpose allocator. They are a fast path for new objects; large objects and refills fall back to shared Eden.
Follow-up: Where does the memory for a TLAB come from?
When is an object promoted to the old generation?+
When its age reaches the tenuring threshold (15 by default), when it is too large for the young spaces, or when the destination survivor space overflows during a copy.
HotSpot can lower the effective threshold adaptively when survivors are crowded, so the same object may be promoted at different ages on different runs. That is why the age is a signal to observe, not a property to design against.
Follow-up: What does premature promotion cost?
Why do survivor spaces have to be one empty?+
Because the young collection is a copying collection: live objects are evacuated into empty space. The empty space is the destination, and the two swap roles after every collection.
Copying into emptiness is what makes compaction free and allocation a pointer bump. The price is that a survivor space is always held in reserve rather than storing data.
Common misconception: That both survivor spaces hold live data at once. Only one of them holds survivors between collections.
What is the card table for?+
It records which 512-byte cards of the heap may contain old-to-young pointers, so a minor GC can scan those cards instead of the whole old generation.
The records are written by the write barrier that runs at reference stores. The barrier costs a few instructions on every such store, which is the ongoing price of keeping young collections independent of the old generation's size.
Common misconception: That the card table tracks references in general. It is a coarse, conservative hint: a dirty card means the collection should look there, nothing more.
Follow-up: Who clears a dirty card, and when?
How would you tell that objects are being promoted too early?+
Full GCs appear under steady load, old-generation occupancy grows with data that should be short-lived, and the GC log shows low ages entering old.
The usual causes are bursty allocation overflowing the survivor spaces (the threshold adapts for you) and genuinely retained data: caches, sessions, listeners. The first wants sizing or less garbage; the second wants a lifetime fix, because no flag removes a reference your code still holds.
Follow-up: What would you check first in the log?