When using golang to write complex projects, it is often useful to use multi-goroutine concurrency scenarios. At this time, it is easy to cause the problem of goroutine leaks due to negligence, and then produce similar memory leaks. This article focuses on the investigation of goroutine leaks, and provides ideas and practices for visual analysis of golang program memory.

Introduction to pprof

pprof is a tool for visualization and analysis of profiling data.
pprof reads a collection of profiling samples in profile.proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).

How to use pprof

Add monitoring code

First, we need to add monitoring code in the golang program, and expose it through the http interface.

package main

import _ "net/http/pprof"
import "net/http"

func main() {
	go func() {
		_ = http.ListenAndServe("0.0.0.0:8081", nil)
	}()
	// your code
}

Note: Exposing pprof on 0.0.0.0 makes it accessible on the network. If you don’t need remote access, prefer 127.0.0.1.

Then we start the program that needs to be analyzed, and we are ready to analyze it.

How to check the memory size of each function/package

By analyzing the size of the memory occupied by each module and function, memory leaks can be found very effectively.

Command line method to generate visual analysis images

go tool pprof -alloc_space -cum http://localhost:8081/debug/pprof/heap

After the command is run, enter web in the console and press Enter, and the default SVG viewer will open, showing the memory usage diagram of each function/package. If you enter web and report an error of Failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in %PATH%, it is because Graphviz is not installed on the computer, which is a component that image generation depends on. The solution is: Open https://graphviz.gitlab.io/download/ and follow the prompts to download and install. After the installation is complete, for Windows, add the bin folder of the Graphviz installation path after setting the environment variable path.

View specific data list in web browser

http://localhost:8081/debug/pprof/heap?debug=1

How to view the number of goroutines created by each function/package

By analyzing the number of goroutines created by each function/package, goroutine leaks can be checked very effectively. If there is goroutine leaks, the number of goroutines in the corresponding functions/packages is astonishing.

Command line method to generate visual analysis images

go tool pprof http://localhost:8081/debug/pprof/goroutine

After the command runs, enter web in the console and press Enter.

View specific data list in web browser

http://localhost:8081/debug/pprof/goroutine?debug=1

Summary

The above is an introduction to the simple use of pprof, I believe it will be helpful to troubleshoot memory leaks and goroutine leaks in golang. If you need more detailed usage, please refer to the official pprof documentation.


Content licensed under CC BY-NC-SA 4.0.

Comments

⬆︎TOP