liveBook · Manning

Heap profiling

As explained in 100 Go Mistakes and How to Avoid Them

A heap profile is sampled data describing heap memory allocation and the current or historical state of heap usage.

A common mistake is to read a heap graph’s total for a function as memory allocated directly by that function. Follow the call chain: the total can include allocations made by functions it calls. section 12.8.1

Heap profiles sample allocations rather than recording every one. The default rate is one sample per 512 KB of heap allocation. Increasing sampling frequency collects more detail but requires more effort. section 12.8.1

Choose a sample type to match the question:

  • alloc_objects and alloc_space count all objects and bytes allocated. section 12.8.1
  • inuse_objects and inuse_space count objects and bytes not yet released. section 12.8.1

Trace an allocation total

A decode call accounts for 1,536 KB, or 6.32% of the heap total, but allocates none of it directly. The next decode call allocates 512 KB directly; another method accounts for the remaining 1,024 KB. section 12.8.1

$ go tool pprof -http=:8080 <file>

Download a profile from /debug/pprof/heap/?debug=0, then use this command with the downloaded file to inspect it in the pprof web UI. section 12.8.1

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains sampling, four sample types, and how to read direct versus downstream allocations in a call graph.
Examples
Traces 1,536 KB through decode calls, separating 512 KB allocated directly from 1,024 KB allocated deeper.
What it assumes
Assumes familiarity with Go applications and heap allocations; provides commands for retrieving and inspecting a profile.
What you can do after
Diagnose/debug with it by retrieving a heap profile and tracing allocation totals through its call graph.
How it connects
Connects heap profiling to CPU profiling, sampling, call graphs, and Go’s pprof web UI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.