Profiling measures where a Go program spends CPU time, memory and goroutine time; tuning is the act of changing code, configuration or runtime settings based on those measurements. Profiling tells you what is slow, and tuning fixes it. Always profile first, tune second, then profile again to confirm the change helped.
Go ships with the tools for both: runtime/pprof and net/http/pprof for profiles, go tool pprof and go tool trace for analysis, go test -bench for measurement, and the GOGC, GOMEMLIMIT and GOMAXPROCS settings for runtime tuning.
A profile is a statistical sample of runtime behaviour. Go supports these built-in profiles:
| Profile | What it shows |
|---|---|
| CPU | Which functions consume CPU time |
| Heap | Live memory and allocation sites |
| Allocs | All past allocations, including freed ones |
| Goroutine | Stacks of all current goroutines |
| Block | Where goroutines wait on synchronisation |
| Mutex | Contention on mutexes |
| Execution trace | A timeline of scheduler, GC and goroutine events |
Profiling is read-only. It never changes your program's logic, although CPU profiling adds a small overhead.
Tuning is any change made to improve speed or resource use. It falls into three groups:
strings.Builder, sync.Pool for short-lived buffers, batching I/O.GOGC (garbage collector target percentage), GOMEMLIMIT (soft memory limit, since Go 1.19) and GOMAXPROCS.Since Go 1.25 the runtime sets GOMAXPROCS from the container CPU limit on Linux, so older advice about adding automaxprocs libraries is less needed on current versions.
The easiest route is a benchmark. Create sum_test.go:
Run it with profiles enabled and open the result:
The browser view includes a flame graph, a call graph and a source view. In the terminal, go tool pprof cpu.out followed by top or list buildString shows the same data as text.
For a server, import net/http/pprof for its side effects and serve it on an internal-only port. Never expose it publicly.
Then capture data from the live process:
Use the execution trace when the CPU profile looks fine but latency is high; it reveals scheduling delays, GC pauses and goroutines blocked on I/O. Go also supports profile-guided optimisation (PGO): put a representative CPU profile at default.pgo in the main package directory and go build uses it to inline and devirtualise hot paths.
The benchmark above shows high allocation cost, because each += copies the whole string. The fix is a code change:
Add import "strings" to the file. Run the benchmark again and compare runs with benchstat:
For memory-bound services, runtime settings are a second lever. GOMEMLIMIT=3GiB makes the collector work harder as the heap nears the limit, which suits containers. Setting GOGC=200 trades more memory for fewer collections. Change one setting at a time and measure again.
| Profiling | Tuning | |
|---|---|---|
| Goal | Find the bottleneck | Remove the bottleneck |
| Output | Data: profiles, traces, benchmarks | Changed code or settings |
| Tools | pprof, trace, go test -bench | Refactoring, sync.Pool, GOGC, GOMEMLIMIT, PGO |
| Risk | Low, small overhead | Can add complexity or bugs |
| When | Before and after every change | Only where data points |
You can, but you will often optimise the wrong code. Measure first so you know which function matters.
It is commonly done, but only on a private listener or behind authentication. The profile endpoints reveal internals and can be used to cause load.
The heap profile reports memory still in use, while allocs reports all allocations since the program started, including ones the garbage collector already freed. Use allocs to cut allocation rates.
Use it for latency, scheduling and GC questions, where the order of events matters. Use pprof for aggregate cost.
Profiling collects evidence and tuning acts on it. Capture CPU, heap and trace data with pprof, change one thing at a time, and compare benchmark results with benchstat. Reach for runtime settings such as GOGC and GOMEMLIMIT after code-level fixes, and let profiles, not guesses, decide where to spend effort.