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"
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: