Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Inside the JVM

    What Garbage Is: Reachability and GC Roots

    An object is live if it's reachable by following references from a GC root; everything else is garbage and can be freed

    Chapter 5 of 103 min read

    The Plain Picture#

    After a show, the cleaners decide what to throw away. The rule: start from the things definitely in use — what the actors are holding, what's pinned to the noticeboard — and follow every string tied to them. Anything you can't reach by following strings from those starting points is rubbish, even if two pieces of rubbish are tied to each other.

    The Real Names#

    • An object is live if it's reachable by following references from a GC root; everything else is garbage and can be freed (Classes, Objects and Memory Ch 4).

    • GC roots include:

      • local variables and parameters in every thread's active stack frames;

      • static fields of loaded classes;

      • active threads themselves;

      • references held by native code (JNI), and objects used as locks (synchronized).

    • Finding live objects is marking: start at the roots and follow every reference (a graph traversal — Graphs I (coming soon)).

      GC ROOTS                              HEAP
      ┌──────────────┐        ┌───────────┐     ┌───────────┐
      │ main's frame │──────▶ │ QuizSvc   │───▶ │ Quiz      │──▶ [Question]   ✔ live (reachable)
      │ static CACHE │──┐     └───────────┘     └───────────┘
      └──────────────┘  │     ┌───────────┐
                        └───▶ │ HashMap   │───▶ [entry] ──▶ [old Note]      ✔ live — maybe a LEAK (Ch 10)
                              └───────────┘
                              ┌───────────┐     ┌───────────┐
                              │  A        │◀──▶ │  B        │                ✘ garbage: they point to each other,
                              └───────────┘     └───────────┘                  but NO root reaches them

    Why not simply count references? Some languages free an object when its reference count reaches zero (Python does this, plus a cycle detector — Python Internals (coming soon)). Pure reference counting can never free cycles like A ↔ B above, and updating counts on every assignment is costly with many threads. Tracing from roots handles cycles naturally.

    The Java memory leak is therefore never "forgot to free" — it's "still reachable by mistake": a static cache that only grows, a listener never removed, a ThreadLocal never cleared in a pool thread (Concurrency Ch 12), a HashMap key whose hashCode changed so its entry can never be removed.

    Check yourself

    4 questions
    1

    What makes an object "live"?

    Show answer

    It is reachable by following references from a GC root.

    2

    Name three kinds of GC root.

    Show answer

    Any three: local variables in active stack frames, static fields, active threads, JNI references, monitor (lock) objects.

    3

    Why does tracing GC handle A ↔ B cycles while pure reference counting doesn't?

    Show answer

    Tracing only keeps what's reachable from roots, so an unreachable cycle is collected; with reference counting, each object in the cycle still has a count of 1.

    4

    What does a memory leak mean in Java?

    Show answer

    Objects that are no longer needed but are still reachable (e.g. held in a static collection), so they can't be collected.