HotShard
Series Β· 6 parts

Java Garbage Collection, from first principles

Six parts that build Java's garbage collectors from the ground up: what counts as garbage, how tracing reclaims it, why the heap is split into generations, how marking runs alongside your program, and how G1 and ZGC keep pauses short.

  1. 1Why Java Needs a Garbage CollectorJava frees an object once no code can reach it any more. Here's why it can't be any simpler than that.~9 min read
  2. 2Tracing: Mark, Then ReclaimFinding garbage is half the job. Turning it into free space, without breaking your program, is the other half.~9 min read
  3. 3Generations and the Write BarrierMost objects die young, so the JVM collects where they're born, and adds a little hidden code to every reference write to make that safe.~9 min read
  4. 4Marking While the Program RunsIf the GC marks while your threads keep changing references, it can free an object that's still in use. Here's exactly how, and the barrier that prevents it.~10 min read
  5. 5G1: Cleaning a Piece at a TimeCut the heap into regions, find the ones that are mostly garbage, and empty a few per pause, sized to the pause time you asked for.~10 min read
  6. 6ZGC, and Choosing a CollectorMove objects while the program is still using them, by checking every reference as it's loaded. Then: which collector should you run?~10 min read