Sub topic 5
The generational heap: eden, survivor spaces, and old generation
This chapter assumes you know that Java objects live on the heap and that the garbage collector (GC) frees objects the program can no longer reach. It also assumes you know the reachability idea from the earlier chapters. Here we look at how HotSpot divides the heap into generations, how an object moves between them, and which flags control that movement.
Why divide the heap at all
Most objects die young. Think of a loop iteration's temporary strings, iterators, or request-scoped DTOs. A small share of objects, such as caches, connection pools, and session state, live for the whole run. This observation is called the weak generational hypothesis.
A collector that scanned the whole heap on every cycle would repeatedly trace long-lived data that never changes. Generational collectors avoid that by splitting the heap by object age:
- The young generation holds new objects. It is small and collected often. Because most objects in it are already dead, a collection is cheap.
- The old generation (also called tenured) holds objects that survived several young collections. It is large and collected rarely.
A collection of the young generation is a minor GC (or young GC). A collection that includes the old generation is a major GC or full GC. The exact meaning of these names varies by collector.
Eden and the survivor spaces
In the classic layout used by the Serial and Parallel collectors, the young generation has three parts:
- Eden is where almost all new objects are allocated.
- Survivor space 0 and survivor space 1 (S0 and S1, also "from" and "to") hold objects that survived at least one minor GC.
At any moment, one survivor space is in use and the other is empty. The empty one is the destination for the next collection.
Allocation in eden is fast. Each thread gets a private chunk of eden called a thread-local allocation buffer (TLAB). Allocating an object means advancing a pointer inside the TLAB ("bump-pointer allocation"), with no locking. When eden has no room for the next allocation, the JVM triggers a minor GC.
What a minor GC does
A minor GC uses a copying algorithm. It does not walk dead objects at all. It follows these steps:
- Pause the application threads (a stop-the-world pause).
- Find live objects in eden and in the in-use survivor space, starting from the GC roots.
- Copy each live object to the empty survivor space, and increase its age by one.
- Promote an object to the old generation instead if it has reached the tenuring threshold, or if it does not fit in the survivor space.
- Declare eden and the old source survivor space empty. The two survivor spaces swap roles.
The cost of a minor GC depends on the amount of live data, not on the amount of garbage. If 99% of eden is dead, the collector copies only the remaining 1%.
Age and the tenuring threshold
Each object's age is stored in a few bits of its header. Four bits are available, so the maximum age is 15. The flag -XX:MaxTenuringThreshold sets the limit. It defaults to 15 for the Serial, Parallel, and G1 collectors.
The threshold is a maximum, not a fixed rule. HotSpot adjusts the threshold on the fly. After a minor GC, if the survivors take up more than -XX:TargetSurvivorRatio percent of the survivor space (default 50), the JVM lowers the threshold so that objects are promoted sooner. This protects the survivor space from overflowing.
Promotion and the old generation
Objects reach the old generation in three ways:
- They reach the tenuring threshold.
- They are forced out early because the survivor space is too small. This is called premature promotion and is a common tuning problem.
- They are very large and are allocated directly in the old generation. The details depend on the collector. G1 calls objects that take at least half a region humongous and allocates them in dedicated old-generation regions.
Garbage in the old generation is reclaimed by an old-generation collection. In Serial and Parallel this is a full GC that pauses the application for the whole collection, and it is much more expensive than a minor GC. The goal of generational tuning is to keep short-lived objects from leaking into the old generation, because that is what makes full GCs frequent.
The old-to-young reference problem
A minor GC should not scan the whole old generation, but an old object can point to a young one. If the collector ignored that reference, it would free a live young object.
HotSpot solves this with a card table. The old generation is divided into small "cards" (512 bytes). When code writes a reference field, a write barrier marks the card containing that field as dirty. During a minor GC, the collector scans only dirty cards and treats the young objects they reference as roots.
This has two practical consequences. Writing references in old objects has a small per-write cost. A minor GC also gets slower as more old objects are changed to point at young ones.
Sizing the generations
Several flags control the generation sizes for Serial and Parallel:
| Flag | Meaning | Default |
|---|---|---|
| `-Xmn<size>` | Fixed size of the young generation | Derived from `NewRatio` |
| `-XX:NewRatio=n` | Old size divided by young size | 2 (young is one third of the heap) |
| `-XX:SurvivorRatio=n` | Eden size divided by one survivor space | 8 (eden 80%, each survivor 10% of young) |
| `-XX:MaxTenuringThreshold=n` | Maximum age before promotion | 15 |
| `-XX:TargetSurvivorRatio=n` | Survivor occupancy that triggers a lower threshold | 50 |
A larger young generation means minor GCs occur less often, and more objects have time to die before they are collected. Each pause may also be longer if more data survives. A smaller young generation gives frequent short pauses and more premature promotion. Fixing the young size with -Xmn is usually best done only when GC logs show a clear problem.
G1 and ZGC do it differently
The G1 collector, the default since JDK 9, keeps generations but not as fixed contiguous areas. It divides the heap into equal-sized regions. Each region is labeled eden, survivor, or old, and the labels change over time. G1 sizes the young generation adaptively to meet its pause-time goal (-XX:MaxGCPauseMillis, default 200 ms). Because of this, -Xmn and NewRatio interfere with that goal, and G1 documentation advises against setting them.
ZGC in JDK 11 to JDK 20 has no generations. Generational ZGC arrived in JDK 21. The age-based tuning in this chapter applies mainly to Serial, Parallel, and G1.
Observing generations in action
The following program allocates 2 GB of short-lived arrays in total, but keeps only one in every thousand. Most objects die in eden, and a small amount survives.
import java.util.ArrayList;
import java.util.List;
public class GenerationDemo {
public static void main(String[] args) {
List<byte[]> longLived = new ArrayList<>();
for (int i = 0; i < 200_000; i++) {
byte[] temp = new byte[10 * 1024]; // 10 KB, dies right away
if (i % 1000 == 0) {
longLived.add(temp); // kept reachable
}
}
System.out.println("Retained " + longLived.size() + " arrays");
}
}Run it with a small, fixed heap and the Serial collector so the layout matches this chapter:
java -Xms64m -Xmx64m -Xmn16m -XX:+UseSerialGC -Xlog:gc*,gc+age=trace:file=gen.log GenerationDemoIn gen.log, look for lines containing Pause Young. Each shows heap occupancy before and after, in the form before->after(total). The "after" value stays small, because only the retained arrays survive. The gc+age=trace output prints a table of how many bytes exist at each age after every minor GC. It shows survivors accumulating at ages 1, 2, 3, and so on.
Common problems and what they look like
- Premature promotion. Survivor spaces are too small for the live set of a request burst. Old-generation usage climbs between full GCs even though the objects die soon after. In the log, young GCs show a large promoted amount, and the age table never gets past age 1 or 2. Fixes: a larger young generation, a larger survivor space (lower
SurvivorRatio), or fewer short-lived allocations. - Frequent minor GCs. Eden is small relative to the allocation rate. Each pause is short but there are many. A larger young generation reduces the count.
- Long minor GCs. Many objects survive each cycle, perhaps because a large buffer or cache is being filled. Time is spent copying, not freeing.
- Full GCs with little reclaimed. The old generation is filled with live data. This is a sizing problem or a leak, and it is covered in later chapters.
Exercise
A service runs with the Parallel collector. Its GC log shows minor GCs every second, each promoting about 40 MB to the old generation, while the heap's live data after a full GC stays under 100 MB. Full GCs happen every few minutes. What is the likely cause, and what two changes would you try first?
Show answer
The likely cause is premature promotion: short-lived objects survive a minor GC while still in use, but the survivor spaces are too small to hold them, so they overflow into the old generation, where they die and later force full GCs. The small live set after a full GC confirms that most of the promoted data is garbage. Try enlarging the young generation (for example with -Xmn) so objects have more time to die in eden, and enlarge the survivor spaces (lower -XX:SurvivorRatio). Check the effect with -Xlog:gc+age=trace, and look for survivors reaching higher ages before promotion.