banner
TerryHu

Terry's Site

bilibili

Using pprof to diagnose performance issues in Golang services

If you find that the CPU/memory usage is too high, or the request response is slow, and you want to know which part of the code is causing the problem, pprof is used to help you with this.

  1. First, you need to import the pprof library in advance. It's simple; just add a few lines of code to your server code.
import (
	"net/http"
	_ "net/http/pprof"
)

func main() {
	go func() {
		http.ListenAndServe("localhost:8080", nil)
	}()
	// Main process
}
  1. Click on the profile in the image below, which will collect 30 seconds of data and save it to the profile file.

Pasted image 20250108223541

Then run

go tool pprof -http :8081 ./profile
  1. Open http://localhost:8081/ui/

Pasted image 20250107105500

In the upper left corner, you can select the legendary flame graph (Flame Graph):

Pasted image 20250107105539

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.