awesome-everything RU
↑ Back to the climb

Performance

GC basics: what the runtime taxes you for

Crux Garbage collection frees heap memory automatically — but the work shows up as pauses and CPU. Allocation rate, not heap size, drives GC-driven tail latency.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at junior altitude — the surface
◷ 12 min

A service’s p99 latency spikes to 800 ms every few seconds. CPU looks fine. No slow queries. Opening the GC log reveals pauses of 600–700 ms every 4 seconds — the service allocates 1 GB/sec and the runtime is stopping the world to clean up.

What a garbage collector does

A garbage collector trades CPU for memory safety: it walks the heap, marks what is reachable from roots (CPU registers, thread stacks, global variables), and reclaims the rest. You do not call free(); the runtime does it for you. The cost is that the runtime must spend CPU time on bookkeeping — and it sometimes pauses your application to do so.

Three knobs are always in tension:

  • Pause time — how long the application stops while GC works.
  • Throughput — how much CPU the collector takes away from your code.
  • Heap size — how much overhead memory the collector needs to work efficiently.

You cannot minimise all three at once.

The kitchen metaphor

Think of your service as a restaurant kitchen. Cooks (your code) produce dirty dishes (allocations). A dishwasher (GC) cleans them. If cooks generate dishes faster than the dishwasher can clean, dishes pile up — the kitchen must stop and clear the sink. A modern concurrent dishwasher cleans while cooks work; an old stop-the-world dishwasher makes everyone wait. Even the best dishwasher costs water and electricity (CPU). The fast kitchen does not come from a magical dishwasher — it comes from cooks reusing plates.

Why allocation rate matters more than heap size

Total heap size says how much memory the program holds at any moment. Allocation rate says how often new memory is requested. GC cycles are triggered proportionally to how fast garbage accumulates — which is the allocation rate, not the heap size.

ScenarioHeap sizeAlloc rateGC pause frequency
Large steady cache4 GB50 MB/sEvery ~80 s, short pauses
High-throughput API100 MB1 GB/sEvery ~0.1 s, frequent spikes

A service that holds 4 GB but allocates slowly may barely see GC pauses. A service that holds 100 MB but allocates 1 GB/s churns GC constantly. The lever for tail latency is allocation rate, not heap size.

The mark-sweep cycle

Every tracing GC follows the same skeleton:

  1. Root scan — briefly pause, identify roots (registers, stacks, globals).
  2. Mark — walk the object reference graph from roots; mark every reachable object.
  3. Sweep — reclaim memory for every object not marked.
  4. Optionally compact — move live objects together to defragment.
  5. Update references — fix pointers to moved objects.
  6. Resume — application runs at full speed until the next cycle is needed.

The naive version pauses the application for all of steps 1–5. A 32 GB heap might take seconds — unusable for any latency-sensitive service. Modern collectors reduce or eliminate most STW phases.

Concurrent vs stop-the-world GC

A stop-the-world (STW) collector pauses all application threads while it works. Simple to implement; pauses grow with heap size.

A concurrent collector runs most of its work alongside your application threads, so user-visible pauses are short (sub-millisecond) instead of long (tens to hundreds of milliseconds). It needs write barriers — small code snippets that run on every reference write to keep the collector informed. The barrier costs ~2–10% CPU; the payoff is short pauses.

Why this works

All modern concurrent GCs still have some stop-the-world phases — root scanning, weak-reference processing, remap. Concurrent collectors minimise STW but cannot eliminate it. Always look at GC logs for actual pause distributions, not vendor headlines.

Quiz

A service's p99 latency spikes correlate with GC pauses. What is the FIRST place to look for the fix?

Quiz

Why is a concurrent GC preferred over a stop-the-world GC for production services?

Order the steps

Order the conceptual stages of a typical garbage-collection cycle:

  1. 1 Identify roots — registers, thread stacks, global variables
  2. 2 Mark — walk from roots, mark every reachable object
  3. 3 Sweep — reclaim memory of objects not marked
  4. 4 Optionally compact — move live objects together to defragment
  5. 5 Update references to moved objects so the application still works
  6. 6 Resume application full-speed until the next cycle is needed
Complete the analogy

Fill in the blank: garbage collection trades CPU for memory _______ — you do not have to track every allocation yourself, but the runtime pays for the bookkeeping with cycles that could have gone to your code.

Recall before you leave
  1. 01
    In one paragraph: why does allocation rate matter more for GC-driven tail latency than total heap size?
  2. 02
    Name the three-way tradeoff every GC must navigate, and give one example of a collector that optimises for each extreme.
Recap

A garbage collector marks reachable objects and reclaims the rest, trading CPU cycles for memory safety. The three knobs — pause time, throughput, and heap size — cannot all be minimised at once. Allocation rate drives GC cycle frequency more than heap size does: a small service that allocates 1 GB/s sees far more GC pressure than a large cache-heavy service at 50 MB/s. Modern concurrent GCs do most of their work alongside application threads, keeping pauses under 1 ms, but still have short stop-the-world phases for root scanning. The first lever for GC-driven tail latency is always the allocation profile, not the collector choice.

Connected lessons
appears again in159
Continue the climb ↑GC algorithms: generational, concurrent, and per-runtime
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.