pprof
Go profiling tooling that samples CPU, heap, and goroutine state, exposed over HTTP or written to files.
Official Documentation ↗runtime/pprof + net/http/pprof. Check the official docs for the latest API surface.Installation
go install github.com/google/pprof@latestMethods
No methods match this filter.
Go profiling tooling that samples CPU, heap, and goroutine state, exposed over HTTP or written to files.
Overview
pprof answers where CPU time, allocations, and contention actually go. Go binaries can expose profile endpoints through a single blank import, and go tool pprof fetches, symbolizes, and renders them as top lists, call graphs, and flame graphs.
The same profiles can be captured programmatically with runtime/pprof for benchmarks and batch jobs. Heap, goroutine, block, and mutex profiles complement CPU sampling, together covering the four golden signals of Go performance.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
net/http/pprof endpoint— Registers /debug/pprof handlers on the default mux for live profile collection.CPU profile— Samples on-CPU stacks for a fixed window; the same command opens the interactive analyzer.runtime/pprof API— Programmatic profiling for benchmarks and one-off runs writing profiles to files.profile endpoints— Heap snapshots, goroutine dumps, and blocking or lock contention profiles by endpoint.
Usage Notes
- Profile real load: CPU profiles during idle periods show nothing worth optimizing.
- Compare before and after profiles with pprof -base to prove a change worked.
- Goroutine dumps with debug=1 are the fastest route to spotting leaked goroutines.
Common Pitfalls
- Leaving pprof bound to 0.0.0.0 without auth exposes internals and a DoS surface.
- heap shows the live view; use allocs to find total allocation pressure over time.
- mutex and block profiles are empty until their runtime sampling rates are enabled.
FAQ
- What is the difference between net/http/pprof and runtime/pprof?
- net/http/pprof exposes live endpoints by side effect of importing it, ideal for servers. runtime/pprof writes profiles to files programmatically, ideal for benchmarks and batch jobs. Both produce the same profile format.
- Is it safe to enable in production?
- Yes, with care: bind the listener to localhost or an internal interface, put it behind auth in front of a proxy, and sample for short windows. CPU profiling adds modest overhead only while a profile is being collected.
- How do I read a mutex or block profile?
- Call runtime.SetMutexProfileFraction(1) or runtime.SetBlockProfileRate(1) first, otherwise those endpoints stay empty. Then read them like CPU profiles: the samples point at where contention or blocking is paid.
Report a problem with this API docOpens a pre-filled issue on GitHub.