Detailed Explanation of Generational Garbage Collection Mechanism in Java
-
Generational Hypotheses and Design Philosophy
- Weak Generational Hypothesis: The vast majority of objects are short-lived, with very brief lifecycles.
- Strong Generational Hypothesis: Objects that survive more garbage collection cycles are less likely to become garbage.
- Based on these two hypotheses, the JVM divides heap memory into different generations and adopts different collection strategies.
-
Memory Generational Structure
The heap memory is divided into three main areas:-
Young Generation
- Eden Space: Newly created objects are first allocated here.
- Survivor Spaces (two): From Survivor and To Survivor, holding objects that survive Minor GC.
- Default ratio: Eden:From:To = 8:1:1
-
Old Generation
- Holds long-lived objects.
- Large objects may also be directly allocated in the Old Generation.
-
Metaspace (JDK8+)
- Replaces the Permanent Generation, storing class metadata information.
-
-
Object Allocation and Promotion Process
-
New objects are preferentially allocated in the Eden Space.
-
When the Eden Space is full, a Minor GC is triggered:
a) Mark live objects in Eden and the From Survivor space.
b) Copy the surviving objects to the To Survivor space.
c) Clear Eden and the From Survivor space.
d) Swap the roles of the From and To Survivor spaces. -
Conditions for object promotion to the Old Generation:
- Age Threshold: An object survives a certain number (default 15) of Minor GCs while in the Survivor spaces.
- Large objects go directly to the Old Generation.
- When the total size of objects of the same age in a Survivor space exceeds half of that Survivor space's capacity, objects with an age greater than or equal to that age are promoted directly.
-
-
Types of Garbage Collection
-
Minor GC / Young GC
- Collects only the Young Generation.
- Uses the Copying algorithm, resulting in short pause times.
- Trigger condition: Eden Space is out of memory.
-
Major GC / Old GC
- Collects only the Old Generation.
- Often confused with Full GC, but actually refers specifically to Old Generation collection.
-
Full GC
- Collects the entire heap, including Young Generation, Old Generation, Metaspace, etc.
- Results in longer pause times; frequent occurrences should be avoided.
- Trigger conditions: Old Generation out of memory, Metaspace out of memory, System.gc() call, etc.
-
-
Garbage Collection Algorithm Combinations
-
Young Generation: Copying Algorithm
- Advantages: No memory fragmentation, high efficiency.
- Disadvantages: Wastes a portion of memory space.
-
Old Generation: Mark-Sweep or Mark-Compact Algorithm
- Mark-Sweep: Generates memory fragmentation but is fast.
- Mark-Compact: Avoids fragmentation but requires moving objects.
-
Common Combinations:
- Serial + Serial Old: Single-threaded, suitable for client applications.
- Parallel Scavenge + Parallel Old: Throughput prioritized.
- ParNew + CMS: Low latency, response time prioritized.
- G1: Regional collection, predictable pause times.
-
-
Related JVM Parameters
- -Xms/-Xmx: Heap initial size / maximum size
- -XX:NewRatio: Ratio of Old Generation to Young Generation
- -XX:SurvivorRatio: Ratio of Eden to Survivor spaces
- -XX:MaxTenuringThreshold: Age threshold for object promotion to Old Generation
- -XX:+PrintGCDetails: Print detailed GC logs
-
Optimization Suggestions
- Adjust generational ratios based on application characteristics.
- Avoid creating excessively large object arrays.
- Set heap size appropriately to avoid frequent Full GC.
- Choose a garbage collector suitable for the application scenario.