Skip to content

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

    Java / Inside the JVM

    How Collectors Work: Mark, Sweep, Compact, Copy

    Throughput — the percentage of time spent running the application rather than collecting (batch jobs care).

    Chapter 7 of 104 min read

    The Plain Picture#

    Three ways to clean a messy room:

    • Sweep: throw away the rubbish and leave everything else where it is. Fast, but the free spaces are scattered — a big new box may not fit anywhere.

    • Compact: throw away the rubbish, then push everything you keep to one side, so all free space is one big block. Slower, tidier.

    • Copy: carry everything you keep into a second, empty room, then declare the first room empty. Fast when you keep little; needs a spare room.

    The Real Names#

    AlgorithmStepsGoodBadUsed for
    mark–sweepmark live objects; free the rest in placeno movingfragmentation: free memory in scattered holes(older) old-generation collectors
    mark–compactmark; slide live objects togetherno fragmentation, no spare spacemoving objects and fixing every reference to them is expensiveold generation
    copyingcopy live objects to a fresh areavery fast when few survive; no fragmentationneeds spare space; expensive when many surviveyoung generation (Ch 6)

    Stop-the-world (STW): while a collector moves objects or examines roots, it must stop all application threads — otherwise threads would read references to objects that are being moved. Every STW pause is a moment when no request is served (Part 0's problem 1: 800 ms spikes = long pauses).

    Concurrent collection: modern collectors do most of their marking (and even compaction) while application threads keep running, keeping STW pauses very short. It's hard, because the application keeps changing references during marking; collectors use barriers — small pieces of code the JIT inserts around reference reads/writes — to track those changes. The trade-off: some CPU overhead and throughput for much shorter pauses.

    Two goals that pull against each other:

    • Throughput — the percentage of time spent running the application rather than collecting (batch jobs care).

    • Latency — the length of individual pauses (APIs care: pauses show up as slow requests at the high percentiles — "p99 latency", Scalability Basics (coming soon)).

    Check yourself

    4 questions
    1

    Mark–sweep vs mark–compact vs copying — one downside each?

    Show answer

    Mark–sweep: fragmentation. Mark–compact: expensive moving and reference updating. Copying: needs spare space and is slow when many objects survive.

    2

    What is a stop-the-world pause, and why is it needed?

    Show answer

    All application threads are paused so the collector can safely examine and move objects without references changing underneath it.

    3

    What do concurrent collectors do differently, and what do they pay for it?

    Show answer

    They do most work while the application runs, keeping pauses short; they pay with CPU overhead (barriers, GC threads) and some throughput.

    4

    Throughput vs latency — which does an API server usually care about more?

    Show answer

    Latency (short pauses), because pauses directly become slow requests.