如何在Go程序中使用pprof

Cod*_*for 7 go google-perftools pprof

如何在Go程序中使用pprof?

有一个名为net/http/pprof的Go包,但我无法使用它.

该文件说go tool pprof http://localhost:6060/debug/pprof/heap,这是行不通的.

而且,以下_是什么意思?

import _ "net/http/pprof"

tif*_*fon 9

根据您的评论,问题可能是您没有使用正确的端口号.

如果您正在运行http服务器http://localhost:9997,那么我认为您要运行以下命令http://localhost:9997:

$ go tool pprof http://localhost:9997/debug/pprof/heap
Run Code Online (Sandbox Code Playgroud)

根据net/http/pprof pkg doc页面,如果您的应用程序已在运行http服务器,则无需启动它,只需要import _ "net/http/pprof"在程序中包含某个地方.http://localhost:6060是作为示例启动的服务器,主机和端口是任意的.

import _ "net/http/pprof"表示已导入包但您不使用其任何导出的标识符.根据go语言规范,这将仅为其副作用导入包.我认为,这些副作用涉及在包的源文件中定义的init()函数的执行,显然是注册变量.

此外,您可能会发现此博文有用:

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