banner
TerryHu

Terry's Site

bilibili

Is your Golang backend service down? Just use pprof.

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

  1. First, you need to import the pprof library in advance. It's simple; just add a few lines of code to the 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, and it 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.