6 · Advanced · lesson 39 of 41

Garbage Collection

How unreachable objects are reclaimed.

Modern collectors (G1, ZGC, Shenandoah) divide the heap into generations. Most objects die young, so a fast 'minor GC' sweeps the young generation. Long-lived objects are promoted to the old generation, which is collected less often but more thoroughly.

Java
# Common tuning flags
-Xms512m -Xmx4g           # initial / max heap
-XX:+UseG1GC              # pick collector
-XX:+PrintGCDetails       # log GC
Loading 3D scene…
Key takeaways
  • ✓You don't free memory — you make objects unreachable.
  • ✓Young/old generations exploit the 'weak generational hypothesis'.