JVM Internals & Memory
GC Fundamentals
- 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
OutOfMemoryErrorusually 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).
| Term | Meaning |
|---|---|
| Stop-the-world (STW) | application threads paused during collection work |
| Parallel | multiple GC threads do the work |
| Concurrent | GC works while application threads run — expensive, never total |
| Moving / evacuating | live objects relocate; addresses aren't stable |
| Compacting | survivors end up contiguous — no fragmentation |
| Exact | the JVM always knows pointer vs int — enables safe moving |
allocation → Eden (TLAB bump-pointer)
Eden fills → YOUNG GC: live objects → Survivor S0/S1 (age++)
age > threshold (~6–15) → 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).