Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pprof in Go program

How to use pprof in Go program?

There is a Go package named net/http/pprof,but I can't use it.

The document says go tool pprof http://localhost:6060/debug/pprof/heap ,which does not work.

And,what does the below _ mean?

import _ "net/http/pprof"

like image 523
Codefor Avatar asked Dec 04 '12 09:12

Codefor


People also ask

What is Pprof in Golang?

pprof is a tool for visualization and analysis of profiling data.

How do you analyze Pprof data?

The pprof package will register its endpoints right below the web server root at /debug/pprof. In order to analyze the currently used memory, just select the inuse indices (bytes or object counts): inuse_space: memory allocated but not yet released. inuse_objects: objects allocated but not yet released.

Which of the following package can be used to find memory usage for Go program?

heap: Heap profile reports memory allocation samples; used to monitor current and historical memory usage, and to check for memory leaks.


1 Answers

Based on your comment, the issue might be that you're not using the correct port number.

If you are running an http server at http://localhost:9997, then I think you want to run the command with http://localhost:9997:

$ go tool pprof http://localhost:9997/debug/pprof/heap

According to the net/http/pprof pkg doc page, if your application is already running an http server you do not need to start one and only need to include the import _ "net/http/pprof" somewhere in your program. http://localhost:6060 is the server started as an example and the host and port are arbitrary.

import _ "net/http/pprof" means the package is imported but you do not use any of its exported identifiers. According to the go language spec, this will import the package solely for its side effects. These side effects involve, I think, the execution of the init() functions defined in the package's source files and, apparently, registered variables.

Also, you might find this blog post helpful:

http://blog.golang.org/2011/06/profiling-go-programs.html

like image 85
tiffon Avatar answered Oct 08 '22 13:10

tiffon