---
source: https://iinventedlight.com/library/notes/go-pprof.md
title: "Record a Go CPU or heap profile with pprof and view it with go tool pprof"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go pkg.go.dev); CC-BY-4.0 (The Go Blog); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go pprof profiles"
tags: [go, pprof, profiling, cpu-profile, heap]
client: Librarian
sources:
  - id: S1
    title: "pprof package"
    publisher: Go pkg.go.dev
    url: https://pkg.go.dev/runtime/pprof
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Profiling Go Programs"
    publisher: The Go Blog
    url: https://go.dev/blog/pprof
    accessed: 2026-11-19
    license: "CC-BY-4.0"
---
# 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

- [go-race-detector](https://iinventedlight.com/library/notes/go-race-detector/)
- [go-fuzzing](https://iinventedlight.com/library/notes/go-fuzzing/)
- [go-profile-guided-optimization](https://iinventedlight.com/library/notes/go-profile-guided-optimization/)

## Claims

- id: C1
  claim: StartCPUProfile begins a CPU profile written to a file.
  source: S1
  evidence: "if err := pprof.StartCPUProfile(f); err != nil {"

- id: C2
  claim: Stop CPU profiling with StopCPUProfile, often via defer.
  source: S1
  evidence: "defer pprof.StopCPUProfile()"

- id: C3
  claim: go tool pprof interprets the resulting profile.
  source: S2
  evidence: "go tool pprof to interpret the profile"

## Sources

- [S1] pprof package. Go pkg.go.dev. https://pkg.go.dev/runtime/pprof . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Profiling Go Programs. The Go Blog. https://go.dev/blog/pprof . License: CC-BY-4.0. Accessed 2026-11-19.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
