1. The Hook (The "Byte-Sized" Intro)
- In a Nutshell: Java has 7 GC collectors: Serial (single-threaded, small apps), Parallel (throughput, multi-threaded), CMS (low latency, deprecated), G1 (default Java 9+, balanced), ZGC (ultra-low latency, <10ms), Shenandoah (concurrent compaction), Epsilon (no-op, testing). G1 = best default (predictable pauses, large heaps). ZGC/Shenandoah = ultra-low latency (<10ms pauses). Parallel = batch processing (high throughput).
- Trade-off: Throughput vs Latency vs Footprint.
- Enable: -XX:+UseG1GC, -XX:+UseZGC, etc.
Think of house cleaning services. Serial = you alone (slow but simple). Parallel = cleaning crew (fast but expensive). G1 = smart crew (cleans messiest rooms first). ZGC = robots (clean while you sleep, barely notice). Each has trade-offs: cost vs speed vs convenience!
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy
- Serial GC: One janitor (simple, slow)
- Parallel GC: Many janitors (fast, but all stop to clean)
- G1 GC: Smart janitors (clean worst areas first)
- ZGC: Ninja janitors (clean while you work, barely visible)
3. Technical Mastery (The "Deep Dive")
GC Comparison
| Collector | Threads | Pause Time | Throughput | Heap Size | Java Version | Flag |
|---|---|---|---|---|---|---|
| Serial | 1 | Long | Low | Small | All | -XX:+UseSerialGC |
| Parallel | Multi | Medium | High | Medium-Large | All | -XX:+UseParallelGC |
| G1 | Multi | Predictable | Good | Large | 9+ (default) | -XX:+UseG1GC |
| ZGC | Multi | <10ms | Good | Huge | 11+ | -XX:+UseZGC |
| Shenandoah | Multi | Low | Good | Large | 12+ | -XX:+UseShenandoahGC |
| Epsilon | N/A | None | Max | Any | 11+ | -XX:+UseEpsilonGC |
4. Interactive & Applied Code
java
public class GarbageCollectorDemo {
public static void main(String[] args) {
demonstrateCurrentGC();
// Run with different GC flags to see behavior
}
static void demonstrateCurrentGC() {
// Get current GC info
List<GarbageCollectorMXBean> gcBeans =
ManagementFactory.getGarbageCollectorMXBeans();
System.out.println("=== CURRENT GC ===");
for (GarbageCollectorMXBean gcBean : gcBeans) {
System.out.println("Name: " + gcBean.getName());
System.out.println("Collections: " + gcBean.getCollectionCount());
System.out.println("Time: " + gcBean.getCollectionTime() + "ms");
}
}
}
/*
RUN WITH DIFFERENT GCs:
# Serial GC (single-threaded)
java -XX:+UseSerialGC -jar app.jar
- Use: Small apps, single-core, client machines
- Young Gen: Serial
- Old Gen: Serial Mark-Sweep-Compact
# Parallel GC (throughput)
java -XX:+UseParallelGC -jar app.jar
- Use: Batch processing, high throughput priority
- Young Gen: Parallel Scavenge
- Old Gen: Parallel Mark-Compact
# G1 GC (default Java 9+)
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar app.jar
- Use: General purpose, default choice
- Region-based (heap divided into ~2000 regions)
- Prioritizes collecting regions with most garbage
# ZGC (ultra-low latency)
java -XX:+UseZGC -Xmx16g -jar app.jar
- Use: Large heaps (8GB-16TB), latency-sensitive
- Pause times <10ms regardless of heap size
- Concurrent marking, compaction, relocation
# Shenandoah GC
java -XX:+UseShenandoahGC -jar app.jar
- Use: Low latency, concurrent compaction
- Similar to ZGC, slightly different algo
# Epsilon GC (no-op)
java -XX:+UseEpsilonGC -Xmx1g -jar app.jar
- Use: Performance testing, short-lived apps
- NO garbage collection (allocate until OOM)
*/5. The Comparison & Decision Layer
| Choose This | When You Need |
|---|---|
| G1 | Default, balanced, large heaps |
| Parallel | Max throughput, batch jobs |
| ZGC | <10ms pauses, huge heaps (>100GB) |
| Shenandoah | Low latency, concurrent compaction |
| Serial | Small apps, embedded, testing |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "G1 vs ZGC?" Answer:
- G1: Predictable pauses (200ms target), good throughput, default
- ZGC: Ultra-low pauses (<10ms), huge heaps, lower throughput
text
G1 GC:
- Stop-The-World for some phases
- Pause time: ~10-200ms
- Throughput: ~90-95%
- Heap: Up to hundreds of GB
- Use: General purpose
ZGC:
- Concurrent (mostly)
- Pause time: <10ms (even for TB heaps!)
- Throughput: ~85-90%
- Heap: 8GB - 16TB
- Use: Latency-critical appsPro-Tip: G1 regions:
text
G1 divides heap into ~2000 equal-sized regions (1-32MB each)
Region types:
- Eden regions (young)
- Survivor regions (young)
- Old regions (old)
- Humongous regions (objects > 50% region size)
G1 algorithm:
1. Mark live objects
2. Calculate "garbage score" per region
3. Collect regions with most garbage first (Garbage First!)
4. Meet pause time goal (stop when goal reached)