Why JVM Uses Two Survivor Spaces: Boosting GC Efficiency and Memory Utilization
JVM’s generational garbage collection divides the heap into young and old generations, using two Survivor spaces to incrementally age objects, apply a copying algorithm, reduce fragmentation, improve memory utilization, and accelerate short‑lived object reclamation, ultimately enhancing overall GC performance.
JVM Heap Garbage Collection Strategy
JVM adopts generational collection, dividing heap memory into young and old generations based on object lifetimes. In the young generation, most objects are reclaimed each GC cycle, while a few survive and, after reaching an age threshold, are promoted to the old generation where objects have higher survival rates.
1. Object Age Promotion Mechanism
Object age increases incrementally; each time a GC moves a surviving object to another region, its age is incremented by one, as illustrated below:
When an object's age reaches the default threshold of 15, it is moved to the old generation. Objects shuttle between Survivor0 and Survivor1 until they reach this threshold, then they are promoted.
Having two Survivor spaces allows control over object lifecycles, handling short‑lived objects and reducing collection time.
2. Young Generation Copying Algorithm
The young generation aims for rapid reclamation of short‑lived objects, so it uses a copying algorithm. Available memory is split into two equal halves; only one half is used at a time. When that half fills, surviving objects are copied to the other half, and the previously used half is cleared in a single step.
After a GC cycle, the result looks like:
The copying algorithm offers three main advantages:
No memory fragmentation: Surviving objects are placed contiguously in the target Survivor space.
Fast reclamation: Only surviving objects are traversed and copied.
Spatial locality: Contiguous placement improves cache hit rates.
3. Memory Optimization
If only one Survivor space existed, each GC would leave surviving objects occupying the entire space, leading to fragmentation and reduced memory efficiency.
Using two Survivor spaces provides dynamic balance—after each GC, surviving objects are compressed into the alternate space, keeping memory compact—and maximizes utilization by alternating the Survivor areas.
Summary
Having two Survivor spaces brings several benefits:
Higher memory utilization and avoidance of fragmentation.
Improved GC efficiency and reduced pause times.
Controlled object lifecycles, preventing unnecessary promotion to the old generation.
Flexible memory management strategies.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.