Algorithms

Garbage Collection

๐Ÿ“‹ Quick Reference

PropertyValue
PurposeAutomatic memory management
Key ConceptIdentify and reclaim unreachable objects
GenerationsYoung (Eden + Survivors), Old (Tenured), Metaspace
Common GCsG1 (default), ZGC, Shenandoah, Parallel, Serial
Best ForUnderstanding memory behavior, tuning performance
One-liner: Automatic memory management that identifies and reclaims unreachable objects across generational heap regions.

๐ŸŽฎ Interactive Visualizer

Watch how the garbage collector manages memory:

Loading visualizer...
Try these operations:
  1. Create objects - see them allocated in Eden
  2. Trigger Minor GC - watch survivors age
  3. Fill Old Gen - observe Major GC
  4. 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)
Code
Loading syntax highlighter...

Major/Full GC (All Generations)

JAVA(4 lines)
Code
Loading syntax highlighter...

Common Collectors

CollectorFocusPause TimeThroughput
SerialSimplicityLongLow
ParallelThroughputMediumHigh
G1 (default)BalancePredictableGood
ZGCLow latency<10msGood
ShenandoahLow latency<10msGood

โœ… GC Tuning Basics

JVM Flags

BASH(14 lines)
Code
Loading syntax highlighter...

Monitoring

JAVA(15 lines)
Code
Loading syntax highlighter...

๐Ÿงฉ Common Memory Issues

Memory Leak

JAVA(9 lines)
Code
Loading syntax highlighter...

OutOfMemoryError

JAVA(13 lines)
Code
Loading syntax highlighter...

GC Thrashing

JAVA(6 lines)
Code
Loading syntax highlighter...

๐Ÿ”„ Reference Types

JAVA(15 lines)
Code
Loading syntax highlighter...

WeakHashMap Example

JAVA(9 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Relying on Finalize

JAVA(10 lines)
Code
Loading syntax highlighter...

2. Calling System.gc()

JAVA(5 lines)
Code
Loading syntax highlighter...

3. Premature Optimization

JAVA(6 lines)
Code
Loading syntax highlighter...

4. Static Collections Growing Forever

JAVA(11 lines)
Code
Loading syntax highlighter...

๐ŸŽฏ Interview Practice

Test your GC knowledge with 10 curated interview questions:

๐ŸŽค Garbage Collection Interview Mode - Coming Soon


๐ŸŽค Interview Tips

Q: How does garbage collection work in Java?
"

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.

Q: What's the difference between Minor and Major GC?
"

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.

Q: How do you diagnose a memory leak?
"

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.

Q: Why doesn't Java guarantee immediate garbage collection?
"

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


Visualizer: GC from @tomaszjarosz/react-visualizers