Garbage Collection
๐ Quick Reference
| Property | Value |
|---|---|
| Purpose | Automatic memory management |
| Key Concept | Identify and reclaim unreachable objects |
| Generations | Young (Eden + Survivors), Old (Tenured), Metaspace |
| Common GCs | G1 (default), ZGC, Shenandoah, Parallel, Serial |
| Best For | Understanding memory behavior, tuning performance |
๐ฎ Interactive Visualizer
Watch how the garbage collector manages memory:
Loading visualizer...
- Create objects - see them allocated in Eden
- Trigger Minor GC - watch survivors age
- Fill Old Gen - observe Major GC
- See object promotion from Young to Old
๐ง Memory Structure
Generational Heap
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ JVM Heap โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ Young Generation โ Old Generation โ โโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโค โ โ Eden โ S0 โ S1 โ Tenured โ โ โ(From) โ(To) โ โ โโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ Metaspace โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Eden: New objects allocated here S0/S1 (Survivors): Objects that survived GC Tenured: Long-lived objects Metaspace: Class metadata (not part of heap, uses native memory)
Object Lifecycle
1. Allocation: new Object() โ Eden space 2. Minor GC: Live objects โ Survivor space 3. Aging: Objects survive multiple GCs โ age++ 4. Promotion: Age threshold reached โ Old generation 5. Major GC: Old generation collected (slower)
๐ GC Types and Algorithms
Minor GC (Young Gen)
JAVA(7 lines)CodeLoading syntax highlighter...
Major/Full GC (All Generations)
JAVA(4 lines)CodeLoading syntax highlighter...
Common Collectors
| Collector | Focus | Pause Time | Throughput |
|---|---|---|---|
| Serial | Simplicity | Long | Low |
| Parallel | Throughput | Medium | High |
| G1 (default) | Balance | Predictable | Good |
| ZGC | Low latency | <10ms | Good |
| Shenandoah | Low latency | <10ms | Good |
โ GC Tuning Basics
JVM Flags
BASH(14 lines)CodeLoading syntax highlighter...
Monitoring
JAVA(15 lines)CodeLoading syntax highlighter...
๐งฉ Common Memory Issues
Memory Leak
JAVA(9 lines)CodeLoading syntax highlighter...
OutOfMemoryError
JAVA(13 lines)CodeLoading syntax highlighter...
GC Thrashing
JAVA(6 lines)CodeLoading syntax highlighter...
๐ Reference Types
JAVA(15 lines)CodeLoading syntax highlighter...
WeakHashMap Example
JAVA(9 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Relying on Finalize
JAVA(10 lines)CodeLoading syntax highlighter...
2. Calling System.gc()
JAVA(5 lines)CodeLoading syntax highlighter...
3. Premature Optimization
JAVA(6 lines)CodeLoading syntax highlighter...
4. Static Collections Growing Forever
JAVA(11 lines)CodeLoading syntax highlighter...
๐ฏ Interview Practice
Test your GC knowledge with 10 curated interview questions:
๐ค Garbage Collection Interview Mode - Coming Soon
๐ค Interview Tips
"Java uses generational GC. New objects go to Eden. Minor GC copies survivors to Survivor spaces, aging them. After surviving multiple GCs, objects are promoted to Old Gen. Major GC collects Old Gen when full.
"Minor GC collects Young Generation only - fast and frequent. Major/Full GC collects the entire heap including Old Gen - slower and less frequent. Minor uses copying; Major may use mark-sweep-compact.
"Use heap dumps (jmap), profilers (VisualVM, JFR), and GC logs. Look for growing Old Gen despite GC, objects accumulating over time, or specific classes with unexpectedly high instance counts.
"GC timing is a JVM implementation detail. The JVM optimizes for overall performance, not immediate cleanup. Forcing immediate GC would hurt throughput and is rarely what you actually need.
๐ Series Navigation
GC from @tomaszjarosz/react-visualizers