Go ships with a complete profiling toolchain in the standard library and the go command. Use go test -bench with -cpuprofile and -memprofile for benchmarks, net/http/pprof for live services, runtime/trace for scheduling and latency questions, and go tool pprof to read the results. Profile first, change one thing, then measure again with benchstat.
Guessing at hot spots is usually wrong. The tools below show where CPU time, memory allocations, blocking and goroutines actually go, so every optimisation is backed by data.
| Question | Tool | Profile or output |
|---|---|---|
| Where is CPU time spent? | pprof CPU profile | profile |
| Who allocates memory, and how much is still live? | pprof heap profile | heap, allocs |
| Where do goroutines wait on locks or channels? | pprof block and mutex profiles | block, mutex |
| Are goroutines leaking? | pprof goroutine profile | goroutine |
| Why is latency high, or why are cores idle? | runtime/trace | execution trace |
| Does my change make the code faster? | go test -bench plus benchstat | statistics |
| Why does a value escape to the heap? | compiler flag -gcflags=-m | escape analysis |
Start with a benchmark. It gives you a repeatable measurement and an easy way to capture profiles.
b.Loop() is available from Go 1.24 and replaces the older for i := 0; i < b.N; i++ loop. It also keeps the compiler from optimising the benchmarked call away. Run the benchmarks and write profiles:
The -benchmem flag prints bytes and allocations per operation. The browser UI offers a flame graph, a graph view and a source view with per-line costs.
You can also work in the terminal. The most used commands are:
Flat time is spent in the function itself. Cumulative time includes everything it calls. A function with low flat but high cumulative time is usually a caller you should look past, while high flat time marks the real hot spot.
To compare two profiles, for example before and after a change, use go tool pprof -diff_base=old.out new.out.
Importing the package for its side effect registers the profiling handlers. Serve them on a separate, private listener and never on your public port.
Then capture data from the live process:
The heap profile shows live memory by default. Use the alloc_space or alloc_objects sample index to see total allocations since start, which is what you want when reducing garbage collector pressure. For production, consider continuous profiling with low overhead and, since Go 1.21, profile-guided optimisation (PGO): place a representative CPU profile as default.pgo in the main package directory and go build uses it to inline and devirtualise hot calls.
The block and mutex profiles are off by default because they add overhead. Enable them explicitly:
Use the block profile to find goroutines waiting on channels, sync.Cond or select. Use the mutex profile to find contended locks. A goroutine count that only grows in /debug/pprof/goroutine points to a leak, typically a goroutine stuck sending on a channel nobody reads or ignoring context cancellation.
A CPU profile tells you where time goes in aggregate. A trace shows a timeline of goroutines, processors, garbage collection and syscalls, which explains latency spikes and idle cores.
In a service, fetch /debug/pprof/trace?seconds=5. The Go 1.21 and later tracer has low overhead, so short traces are safe on many production systems. Since Go 1.25 a flight recorder API can keep a rolling window of trace data in memory and write it out when something goes wrong.
A single benchmark run is noisy. Run each variant several times and compare statistically:
benchstat (from golang.org/x/perf/cmd/benchstat) reports the change and whether it is statistically significant. Keep the machine quiet, pin versions and compare only like with like.
The CPU profiler samples about 100 times per second and typically costs a few percent. Block and mutex profiling cost more when the rate is high, so keep their rates low in production.
No. The endpoints leak internals and can be used to cause load. Bind them to localhost or an internal network and protect them with authentication.
The heap profile covers memory the Go runtime allocated for objects as of the last completed garbage collection. Stacks, runtime metadata, cgo allocations and memory not yet returned to the operating system are not part of it. Check runtime/metrics or runtime.MemStats for the whole picture.
Use a trace when the program is slow but CPU is not saturated, for example with scheduler latency, lock waits, garbage collection pauses or poor parallelism.
Measure before you optimise. Write a benchmark, capture CPU and memory profiles with go test, expose net/http/pprof privately for live services, and use runtime/trace for latency questions. Read results with go tool pprof, make one change at a time, and confirm the gain with benchstat. For stable production workloads, PGO can add a further improvement for free.