In depth minor gc in Java
- by utk
🔥 What is Minor GC?
Minor Garbage Collection refers to GC events that only affect the Young Generation
(Eden + Survivor spaces) of the JVM heap.
It is fast, happens frequently, and is less expensive than Major GC because the Young
Gen is small and short-lived objects are easy to clean.
🔹 Young Generation Structure Recap
The Young Generation (Young Gen) is part of the heap where new objects are allocated.
It consists of:
diff
CopyEdit
+-----------+------------+------------+
| Eden | Survivor 0 | Survivor 1 |
+-----------+------------+------------+
• Eden: Where all new objects are initially created.
• Survivor Spaces (S0 & S1): Objects that survive GC move here.
• The survivor spaces alternate roles after each GC cycle.
🔹 When Does Minor GC Happen?
Minor GC is triggered when:
1. Eden fills up with new allocations.
2. There isn't enough space to allocate a new object.
3. System.gc() is called (may trigger Minor and Major GC depending on heap
pressure).
🔹 How Minor GC Works – Step by Step
✅ 1. Stop-the-World Pause
All application threads pause (briefly) during a Minor GC.
✅ 2. Mark and Copy Algorithm
• Reachable objects in Eden and the active Survivor space are identified (from GC
roots).
• These live objects are copied into the other Survivor space.
• Objects that survive multiple Minor GCs (e.g., 15 by default) are promoted to Old
Gen.
✅ 3. Eden is Cleared
Once live objects are moved, Eden is wiped clean.
text
CopyEdit
Eden → Survivor 0 → Survivor 1 → Old Gen
🔹 Minor GC Example in Log
text
CopyEdit
[GC (Allocation Failure) 1024K->128K(2048K), 0.0056789 secs]
• 1024K->128K: Heap used before and after GC.
• 0.0056 secs: GC pause time (~5 ms).
• Only Young Gen is affected.
🔹 Advantages of Minor GC
Benefit Why it Matters
Fast Eden is small, copying is quick
Frequent Keeps memory fresh; avoids Old Gen promotion
Mark-and-copy is faster than mark-sweep-
Efficient Algorithm
compact