Sub topic 5
The Generational Heap: Eden, Survivor Spaces, and Old Generation
Assume you know basic Java syntax, object references, and how to run a Java program from the command line. This chapter focuses on how the heap is organized for generational garbage collection. It does not cover every collector implementation in detail.
Why divide the heap into generations?
Most Java objects have short lifetimes. Temporary objects created while processing one request may become unreachable soon afterward. Other objects, such as application configuration or long-lived caches, remain reachable for much longer.
A generational heap uses this observation:
- New objects begin in the young generation.
- Objects that survive several collections may move to the old generation.
- The JVM collects the young generation frequently because it is usually quick to scan.
- The JVM collects the old generation less often because it contains more long-lived objects.
This design reduces the amount of live data that each collection must examine.
Eden and survivor spaces
The young generation commonly contains:
- Eden space, where most new objects are allocated.
- Two survivor spaces, often called S0 and S1.
The JVM uses one survivor space as the source and the other as the destination during a young collection. The roles switch after each collection.
A simplified allocation sequence looks like this:
When Eden becomes sufficiently full, the JVM starts a young collection. It identifies reachable objects in Eden and the active survivor space. Unreachable objects can be reclaimed. Reachable objects are copied into the other survivor space or promoted to the old generation.
The two survivor spaces allow the JVM to compact surviving objects while it copies them. This avoids leaving many small free gaps between objects.
| Area | Typical contents | Collection behavior |
|---|---|---|
| Eden | Newly allocated objects | Collected frequently |
| Survivor spaces | Objects that survived young collections | Objects move between the two spaces |
| Old generation | Long-lived objects | Collected less frequently |
Object aging and promotion
An object has an age that increases when it survives young collections. The JVM can promote an object to the old generation after it reaches a configured age. The exact decision depends on the collector, available survivor capacity, and current allocation patterns.
Promotion is not always based only on one fixed age. If survivor spaces cannot hold all surviving objects, the JVM may promote some objects earlier. This is called premature promotion. It can increase pressure on the old generation and lead to more expensive old-generation collections.
Large objects may also bypass the normal Eden path, depending on the collector and JVM settings. Therefore, the generational model is useful for understanding behavior, but it is not a promise that every object follows exactly the same path.
Seeing allocation pressure
This program creates many short-lived arrays. Most arrays become unreachable after each loop iteration.
public class AllocationDemo {
public static void main(String[] args) {
long total = 0;
for (int batch = 0; batch < 100; batch++) {
for (int i = 0; i < 10_000; i++) {
byte[] temporary = new byte[1024];
total += temporary.length;
}
}
System.out.println("Allocated logically: " + total + " bytes");
}
}Compile and run it with a small heap and GC logging:
javac AllocationDemo.java
java -Xms32m -Xmx32m -Xlog:gc* AllocationDemoThe log format varies by JDK build and collector. Look for young collection events and, when applicable, messages indicating promotion or old-generation activity. The logical allocation total is much larger than the final live memory because most arrays are no longer reachable.
Practical consequences
A high allocation rate does not automatically indicate a memory leak. Short-lived allocations can be handled efficiently if young collections reclaim them promptly. Warning signs include:
- Survivor spaces repeatedly filling.
- Objects being promoted faster than the old generation can reclaim them.
- The old generation growing after full collections.
- Long pauses caused by collections involving much of the heap.
Use GC logs and heap measurements together. A single young collection shows an event, not a trend. Trends across time reveal whether objects are surviving unexpectedly or whether the application simply allocates many temporary values.
Exercise
An application creates many temporary request objects, but its old generation keeps growing after full collections. What should you investigate first?
Show answer
Investigate whether references such as caches, listeners, queues, or thread-local values keep those objects reachable. Also inspect promotion behavior and heap logs to distinguish a leak from insufficient old-generation capacity.