• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Manual memory management (like C’s malloc/free) is error-prone — forgetting to free causes memory leaks, freeing too early causes dangling pointers, and double-freeing causes crashes. In large applications, tracking object lifetimes manually is nearly impossible.

Core Idea

Garbage Collection (GC) automatically reclaims memory occupied by objects that are no longer reachable. The JVM identifies unused objects, reclaims their memory, and compacts the heap to prevent fragmentation. Developers are freed from manual memory management, at the cost of occasional GC pauses.

How It Works

The heap is divided into generations: Young (Eden + Survivor spaces) and Old (Tenured). New objects are allocated in Eden. Minor GC collects the young generation — live objects are copied to Survivor, then eventually promoted to Old. Major GC collects the entire heap. Different collectors (Serial, Parallel, G1, ZGC) use different algorithms (mark-sweep, mark-compact, concurrent).

Visual Explanation

java_gc Heap Heap Young Young Generation Heap->Young Old Old Generation (tenured objects) Heap->Old Meta Metaspace (class metadata) Heap->Meta Eden Eden (new objects) Young->Eden S0 Survivor 0 Young->S0 S1 Survivor 1 Young->S1 Eden->S0 minor GC (copied) S0->S1 swap S0->Old promoted after N cycles

Semantic Network

semantic_gc THIS Garbage Collection MEM Memory Management THIS--MEM built from PERF Performance Tuning THIS--PERF builds into OOP Object References THIS--OOP related LEAK Memory Leaks THIS--LEAK related

Key Properties

  • Generational hypothesis: Most objects die young — optimizing for this yields performance
  • GC pause: “Stop-the-world” events freeze application threads (duration varies by collector)
  • Concurrent collectors: ZGC, Shenandoah, G1 aim for sub-millisecond pause times
  • GC tuning: JVM flags control heap sizes, collector selection, and GC behavior

Connections

Edge Cases & Gotchas

  • System.gc(): Suggests GC but does not guarantee it runs — ignore this call in production
  • Finalization: finalize() is deprecated (Java 9+) — use Cleaner or try-with-resources
  • GC logs: Enable with -Xlog:gc* for tuning — critical for diagnosing memory issues
  • Object resurrection: In finalize(), an object can make itself reachable again (avoid this pattern)