← All notes

Record a Go CPU or heap profile with pprof and view it with go tool pprof

Answer: Write a CPU profile with StartCPUProfile and stop it with StopCPUProfile. Write a memory profile with Lookup("allocs") or Lookup("heap"). Then run go tool pprof to interpret the profile.

Collecting a profile

The runtime/pprof example uses -cpuprofile and -memprofile flags. If the CPU flag is set, create the file and call StartCPUProfile; defer StopCPUProfile() ends it. Lookup("allocs") creates a profile similar to go test -memprofile. Alternatively, use Lookup("heap"). You can also run go test -cpuprofile cpu.prof -memprofile mem.prof -bench ..

The Profiling Go Programs blog uses the same StartCPUProfile call, then go tool pprof on the binary and the profile file. Profiles can then be visualized with the pprof tool. A URL can download live profiles.

Reading the profile

go tool pprof loads the samples. The blog notes a profile with 2525 samples, so it was running for a bit over 25 seconds.

Gotcha

The profiler requires a final call to StopCPUProfile.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go pkg.go.dev, pprof package; The Go Blog, Profiling Go Programs. BSD-3-Clause; CC-BY-4.0. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go pkg.go.dev, pprof package; The Go Blog, Profiling Go Programs. BSD-3-Clause; CC-BY-4.0.

Cite this

Permanent ID: iil:note:xk3adv79

iinventedlight. 2026. "Record a Go CPU or heap profile with pprof and view it with go tool pprof." iinventedlight Library, iil:note:xk3adv79, version of 2026-11-19. https://iinventedlight.com/library/notes/go-pprof/. License: CC-BY-4.0.

@misc{iil_note_xk3adv79,
  author = {{iinventedlight}},
  title = {{Record a Go CPU or heap profile with pprof and view it with go tool pprof}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/go-pprof/},
  note = {Permanent ID iil:note:xk3adv79. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-pprof.md.