How to Use go tool pprof for Performance Profiling

Use go tool pprof to analyze CPU and memory profiles and identify performance bottlenecks in Go applications.

Use go tool pprof to analyze CPU, memory, or block profiles generated by your Go program.

First, run your program with profiling enabled to generate a profile file, then use go tool pprof to visualize the data:

# 1. Generate a CPU profile
export GODEBUG=tracebacklabels=0
go test -cpuprofile=cpu.out ./myapp

# 2. Analyze the profile interactively
go tool pprof cpu.out

# 3. Generate a top list of hotspots
go tool pprof -top cpu.out

In the interactive shell, type top to see the most expensive functions, web to generate a flame graph, or quit to exit.