Heap profile
Find allocation sources and reduce GC pressure.
Most Go performance work is allocation work. The collector is fast, but it is not free, and the cheapest way to spend less time in it is to give it less to do. A heap profile says where the allocations come from.
Two different questions
go test -run XXX -bench BenchmarkListEnvelope -benchtime 5s \-memprofile mem.out ./internal/handlers/go tool pprof -sample_index=alloc_space -http=: mem.out # what allocatedgo tool pprof -sample_index=inuse_space -http=: mem.out # what is still held
- alloc_space is everything allocated over the run, including what was freed immediately. This is the one for garbage pressure: a function allocating a megabyte per request and freeing it still costs you collector time.
- inuse_space is what is still reachable at the sample. This is the one for leaks: memory that should have gone and did not.
Picking the wrong one is the usual mistake. Hunting a leak in alloc_space shows you your busiest function, which is almost never the leak.
Start with the numbers you already have
-benchmem is free and often enough. From a scaffolded Grit API:
BenchmarkHealthCheck-14 11465 ns/op 6878 B/op 25 allocs/opBenchmarkVerifyAccessToken-14 8330 ns/op 3338 B/op 41 allocs/opBenchmarkBindListParams-14 8355 ns/op 7974 B/op 40 allocs/opBenchmarkListEnvelope-14 14790 ns/op 11390 B/op 36 allocs/op
Note that verifying a token allocates more times than building a list envelope while allocating a third of the bytes. Count and size are different costs: many small allocations are mostly collector bookkeeping, one large one is mostly the copy. The profile tells you which you have.
Allocation counts as assertions
Grit scaffolds internal/handlers/perf_test.go, and the reasoning in it is worth more than the numbers:
// A benchmark tells you how long something took on the machine that ran// it, which on a shared CI runner is a number that moves on its own. An// allocation count does not move with the machine or the load, which is// what makes these assertions rather than measurements, and why they can// fail a build when a benchmark cannot.
That is why the budgets exist and the timings do not get asserted. CI runs the benchmarks with -benchtime 1x, purely to prove they still compile and run, and the allocation budgets are the part that can actually fail.
Allocation counts move with the Go toolchain, by more than you would guess. Token verification allocates 56 times under go1.26.9 and 41 under go1.27.2: the same code, a newer runtime. So a budget is set from the oldest toolchain the project supports, because that is the one that allocates most. Set from whatever is on your laptop, a budget passes locally and fails in CI.
Where the allocations usually are
- Growing a slice in a loop. Each growth copies.
make([]T, 0, n)when you know n turns many allocations into one. - String concatenation in a loop. Every
+=allocates a new string.strings.Builderdoes not. interface{}andany. Putting a value in an interface usually heap allocates it. A response built asmap[string]anyallocates once per value.- Taking the address of a local. If a pointer escapes the function, the value moves to the heap.
go build -gcflags=-mprints every such decision.
From the running API
go tool pprof -http=: http://localhost:8080/pulse/api/profile/heap# and the one that answers "what changed", which is usually the questiongo tool pprof -http=: -diff_base before.out after.out
-diff_base is the most useful flag in pprof and the least used. Two captures, one graph, showing only what moved between them.
Try it
One. Run the handler benchmarks with -benchmem and write down allocs/op for each.
Two. Take a memory profile of the one with the most allocations and find the top three sources by alloc_space.
Three. Change one of them: preallocate a slice, or swap a concatenation for a strings.Builder. Re-run and compare allocs/op.
Quick check
Spot a typo? Have an idea?
Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.
Suggest an improvement on GitHub