JVM Internals & Memory

GC Fundamentals

GC finds live objects by tracing from roots (stacks, statics) — everything unreached is garbage by definition. The weak generational hypothesis ("most objects die young") makes collection cheap: young collections touch only survivors.
  • Reachability, not reference counting: cycles are collected for free
  • Weak generational hypothesis: most objects die young; survivors get promoted via survivor spaces
  • Young GC cost ∝ live objects, not allocated — dead objects cost literally nothing to collect
  • Stop-the-world pauses at safepoints; modern collectors shrink STW to milliseconds
  • Card tables / remembered sets track old→young pointers so young GC needn't scan the old gen
  • OutOfMemoryError usually means a leak: reachable-but-unused objects GC cannot touch

The mental model (Optimizing Java ch. 6): GC = mark (trace the object graph from GC roots — thread stacks, static fields, JNI handles) + reclaim (sweep the dead in place, compact the live together, or evacuate live objects to a fresh region). Stop-the-world means application threads pause at safepoints so the heap graph holds still; "concurrent" collectors do most marking while the app runs, at the price of extra bookkeeping (barriers).

GC vocabulary (Optimizing Java's glossary)
TermMeaning
Stop-the-world (STW)application threads paused during collection work
Parallelmultiple GC threads do the work
ConcurrentGC works while application threads run — expensive, never total
Moving / evacuatinglive objects relocate; addresses aren't stable
Compactingsurvivors end up contiguous — no fragmentation
Exactthe JVM always knows pointer vs int — enables safe moving
The generational lifecycle
allocation → Eden (TLAB bump-pointer)
Eden fills → YOUNG GC: live objects → Survivor S0/S1 (age++)
age > threshold (~615) → promotion to Old generation
Old fills → OLD/FULL GC (the expensive one — avoid needing it often)

Because young-collection cost tracks live data only, high allocation rates of short-lived objects are cheap — the design center of idiomatic Java. What hurts: premature promotion (allocation spikes push still-live objects to old before they die → old gen fills with garbage → full GCs), and leaks (a static map that only grows keeps everything reachable). Measure allocation rate and promotion rate before touching flags (Gc Tuning Logging).

Sources
  • Optimizing JavaCh. 6 — Understanding Garbage Collection
  • Optimizing Cloud Native Java (2nd ed.)Ch. 4 — Understanding Garbage Collection
  • Core Java, Volume I: Fundamentals (13th ed.)Ch. 4 — Objects and Classes (object lifecycle)