Understanding the Young Generation and Survivor Spaces in Java HotSpot JVM Garbage Collection
The article explains why the JVM uses a generational heap, describes the structure and operation of the young generation—including Eden and two Survivor spaces—illustrates object lifecycles, the role of Survivor spaces in preventing fragmentation, and outlines related JVM tuning parameters.
1. Why a Young Generation Exists
The heap is divided into generations to improve GC performance; without generational separation, the GC would have to scan the entire heap, but most objects are short‑lived, so isolating them in a young generation allows quick reclamation of space.
2. GC in the Young Generation
The young generation (PSYoungGen) consists of an Eden space and two Survivor spaces (From and To) with a default size ratio of 8:1. New objects are allocated in Eden; after a Minor GC, surviving objects move to a Survivor space. Each time an object survives a Minor GC its age increases, and after reaching a configurable threshold it is promoted to the old generation.
The young‑generation GC uses a copying algorithm: one Survivor space is empty (To) while the other (From) holds objects. During Minor GC, live objects in Eden are copied to To, and objects in From are either copied to To or promoted based on their age. After the collection, Eden and From are cleared and the roles of From and To are swapped, ensuring one Survivor space is always empty.
3. The Life of an Object
An object is created in Eden, may move to Survivor From/To several times, and after reaching the age threshold (e.g., 18 minor GCs) is promoted to the old generation where it lives until it is finally reclaimed.
4. Purpose of the Survivor Spaces
Survivor spaces prevent every surviving object from being immediately promoted to the old generation, which would cause frequent Full GCs. By pre‑filtering objects that survive many Minor GCs, they reduce the number of promotions and thus the cost of Full GC.
5. Why Two Survivor Spaces
Having two Survivor spaces avoids fragmentation: after each Minor GC, live objects are copied into the empty Survivor space, guaranteeing that the occupied Survivor space contains contiguous memory. This continuous layout prevents the heap from becoming fragmented, which would otherwise degrade allocation performance.
6. JVM Parameters Related to the Young Generation
Common tuning flags include:
-XX:NewSize and -XX:MaxNewSize – set the young generation size (often 1/3 or 1/4 of the total heap).
-Xmn – directly specifies the young generation size (Eden + two Survivor spaces).
-XX:SurvivorRatio – defines the Eden‑to‑Survivor size ratio (default 8:1). Example: -XX:SurvivorRatio=4 makes Eden twice the size of each Survivor space.
-XX:+PrintTenuringDistribution – prints the age distribution of objects in Survivor after each Minor GC.
-XX:InitialTenuringThreshold and -XX:MaxTenuringThreshold – set the minimum and maximum ages for promotion to the old generation.
Understanding and tuning these parameters helps control the frequency of Minor and Full GCs, improving Java application performance.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.