Reading GC Traces Under Load

Diagnosing garbage-collection pressure with `GODEBUG=gctrace=1` — a trace line decoded field by field, real healthy and pathological traces side by side, and what `GOGC` measurably buys.

There’s a latency signature that points inside the runtime rather than in front of it: p99 doubles, the graph goes sawtoothed, and everything that usually causes latency is innocent — CPU isn’t pegged, throughput holds, the database is bored. On an allocation-heavy service (every request builds structs, decodes fields, stages entries), that pressure lands on Go’s garbage collector, and when the GC runs more often or steals more CPU from your goroutines, latency moves in exactly that sawtooth shape.

The fastest way to confirm the hunch ships with every Go binary and costs almost nothing: the GC trace. This post is about reading it — and every trace line in it is real output from a workload you can run yourself: a request-shaped allocator (per “request”: a grown []byte plus a 24-entry field map) run in three modes on an Apple M1 Max, Go 1.27, 10 P’s. Not escape analysis (that’s the previous post — it decides whether an allocation happens at all), and not GOMEMLIMIT pacing. Just: turn on gctrace, one line prints per GC cycle, learn to read it like an EKG.


Turning it on without redeploying the world

gctrace is a GODEBUG setting — no recompile, no imports:

GODEBUG=gctrace=1 ./authsvc

In a containerized deployment you rarely restart the fleet on a whim, so the practical options are a canary pod with the env var baked in and a slice of traffic routed to it, or a load-test instance replaying captured traffic.

The gotcha: gctrace writes to stderr, one line per GC cycle, unbuffered and unstructured. Under load that’s a lot of lines (the churn workload below produced ~690 per second). Make sure stderr is captured by your log pipeline, and be ready to grep '^gc ' because other GODEBUG output interleaves on the same stream. It also can’t be toggled at runtime — it’s read once at startup — so treat it as a deploy-time decision, not an incident knob.


Anatomy of one trace line

A real line from the steady-churn workload:

gc 25 @0.036s 6%: 0.18+0.62+0.059 ms clock, 1.8+0.076/0.069/0.001+0.59 ms cpu,
      3->4->0 MB, 4 MB goal, 0 MB stacks, 0 MB globals, 10 P

Field by field:

Two derived quantities aren’t printed — you compute them:


What healthy-but-churning looks like

The steady workload (8 goroutines allocating request-shaped garbage flat out, retaining nothing) produces a trace that is regular — and regularity is most of what “healthy” means:

gc 20 @0.029s 6%: ... 3->3->0 MB, 4 MB goal, ... 10 P
gc 25 @0.036s 6%: ... 3->4->0 MB, 4 MB goal, ... 10 P
    ...8 seconds later...
done: numgc=5514  gc_cpu_frac=0.071

The tells, each visible above:

But one number says this service still has a problem: 5,514 collections in 8 seconds, costing 7.1% of all CPU. Nothing is leaking — the GC is simply sprinting to keep up with a 1.7 GB/s allocation firehose against a 4 MB heap. That’s the frequent-but-cheap disease: no single cycle hurts, and the collector quietly taxes every core. Remember the shape; the fix comparison below is measured against it.


What a leak looks like

Same workload, one change: each request also appends its buffer to a global slice — an unbounded “audit log.” Real trace, sampled:

gc 10 @0.042s 1%: ...   6->7->3 MB,   7 MB goal, ... 10 P
gc 20 @0.162s 0%: ...  25->25->14 MB,  25 MB goal, ... 10 P
gc 30 @0.582s 0%: ...  90->90->52 MB,  92 MB goal, ... 10 P
gc 40 @2.098s 0%: ... 328->328->189 MB, 332 MB goal, ... 10 P

Every healthy tell inverts, and you can read the disease straight off the columns: live heap (H2) climbing 3 → 14 → 52 → 189 MB under steady traffic — something is accumulating. goal chasing it upward, 7 → 25 → 92 → 332 MB, because the pacer always targets a multiple of live. Intervals stretching (0.04s → 0.16s → 0.58s → 2.1s between samples) as the growing goal gives each cycle more runway — which is why “GC is running less often” is not reassuring on its own. And the mark CPU numbers growing with the live set, because every surviving megabyte is future scan work for every future cycle.

A steadily rising H2 under steady load is the classic shape of a leak or an unbounded cache, and no GC tuning fixes it — the next step is the heap profiler (go tool pprof -alloc_space, or -inuse_space for what’s retained), which names the call site doing the accumulating.

The gotcha: high GC frequency and high GC CPU are different diseases, and the two traces above let you tell them apart at a glance. Frequent-but-cheap with a flat live heap = allocation churn — fix by allocating less, or give the heap room with GOGC. Climbing live heap = retention — fix the leak; more heap room just delays the wall. And when allocation outruns the background collector entirely, the runtime drafts your goroutines into mark assist (the x in the x/y/z CPU triple) — that cost lands on individual unlucky requests, which is precisely why GC pain shows up at p99 before it shows up in the mean.


What GOGC measurably buys

GOGC is the pacer’s setpoint: at the default 100, the next GC triggers when the heap is 100% larger than the live set. Raise it and GC runs less often at the cost of a bigger heap:

import "runtime/debug"

// Programmatic — the honest place to put it, next to a comment explaining why.
debug.SetGCPercent(400) // trade heap for fewer GC cycles on this service
# Or via env, no recompile:
GOGC=400 ./authsvc

Here’s the same steady-churn workload at both settings — same machine, same 8 seconds:

What GOGC measurably buys
GOGC=100 GOGC=400
GC cycles 5,499 1,793 (−67%)
GC share of CPU 7.1% 2.4%
Heap goal 4 MB 16 MB
Work done (bytes allocated) 13.9 GB 25.3 GB (+82%)

The last row is the one worth staring at. Freeing the workers from GC taxation — fewer cycles, less mark assist — meant they got 82% more actual work done in the same wall-clock time, for the price of 12 MB of heap. When the diagnosis is frequent-but-cheap churn, GOGC is a spectacular lever.

The gotcha: don’t tune GOGC blind and call it fixed. You’ve traded memory for CPU, and pushed closer to the container’s memory limit — check the trade is affordable, and confirm the trace after the change: frequency down, GC CPU down, assist near zero, live heap still flat. If live heap is rising, you have the other disease, and no pacer setting will save you. (For bounding total memory against a cgroup limit, GOMEMLIMIT — a ceiling, not a ratio — is its own topic and its own post.)

The priority order stands regardless: allocate less first (pool churny buffers, presize maps and slices, keep fmt boxing off hot paths — the escape-analysis post measures those fixes), then tune the pacer with what remains.


A reading checklist

A reading checklist
Symptom in the trace Likely cause First action
@Ts deltas tiny and constant, H2 flat Allocation churn vs. small heap Reduce allocs; consider raising GOGC
Cumulative % climbing over a window Collector stealing CPU Cut allocation rate; profile -alloc_space
Mark-assist (x) non-trivial Allocating faster than background mark Reduce allocs — more heap won’t fix it
H2 rising steadily Leak or unbounded cache Heap profile; find the retained objects
goal far above live, GC still frequent GOGC too low for the workload Raise GOGC (if memory allows)

Key takeaways


Further reading


All trace lines and numbers: a request-shaped allocation workload (8 worker goroutines; per request a grown []byte + 24-entry map) on an Apple M1 Max, Go 1.27, 8-second runs. Machine-specific in absolute terms; the shapes are universal.