使用go tool pprof分析内存泄漏、CPU消耗

go中提供了pprof包来作代码的性能监控,在两个地方有包:html

  • net/http/pprof
  • runtime/pprof

其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。node

使用 net/http/pprof 作WEB服务器的性能监控

若是你的go程序是用http包启动的web服务器,想要查看本身的web服务器的状态。这个时候就能够选择net/http/pprof。
git

   import _ "net/http/pprof"
而后就能够在浏览器中使用http://localhost:port/debug/pprof/ 直接看到当前web服务的状态,包括CPU占用状况和内存使用状况等。
固然,非WEB的也能够用下面方式启动WEB。
在 main 方法中增长
func main() {
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()
下图就是访问该网址的一次截图:
image
 

CPU消耗分析

使用 runtime/pprof 作应用程序性能监控

关键代码:github

import  "runtime/pprof"golang

 

func main() {
    f, err := os.OpenFile("./tmp/cpu.prof", os.O_RDWR|os.O_CREATE, 0644)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()web

注意,有时候 defer f.Close(), defer pprof.StopCPUProfile() 会执行不到,这时候咱们就会看到 prof 文件是空的, 咱们须要在本身代码退出的地方,增长上下面两行,确保写文件内容了。浏览器

pprof.StopCPUProfile() 服务器

f.Close()性能

 

对产生的文件进行分析:

咱们可使用 go tool pprof (应用程序) (应用程序的prof文件) 方式来对这个 prof 文件进行分析。debug

$ go tool pprof HuaRongDao ./tmp/cpu.prof
Entering interactive mode (type "help" for commands)
(pprof)

一些经常使用 pprof 的命令:

top

在默认状况下,top命令会输出以本地取样计数为顺序的列表。咱们能够把这个列表叫作本地取样计数排名列表。

(pprof) top
2700ms of 3200ms total (84.38%)
Dropped 58 nodes (cum <= 16ms)
Showing top 10 nodes out of 111 (cum >= 80ms)
      flat  flat%   sum%        cum   cum%
     670ms 20.94% 20.94%      670ms 20.94%  runtime.mach_semaphore_signal
     580ms 18.12% 39.06%      590ms 18.44%  runtime.cgocall
     370ms 11.56% 50.62%      370ms 11.56%  runtime.mach_semaphore_wait
     360ms 11.25% 61.88%      360ms 11.25%  runtime.memmove
     210ms  6.56% 68.44%      580ms 18.12%  golang.org/x/mobile/gl.(*context).DoWork
     120ms  3.75% 72.19%      120ms  3.75%  runtime.usleep
     110ms  3.44% 75.62%      110ms  3.44%  image/png.filterPaeth
     100ms  3.12% 78.75%      160ms  5.00%  compress/flate.(*decompressor).huffSym
     100ms  3.12% 81.88%      100ms  3.12%  image/draw.drawNRGBASrc
      80ms  2.50% 84.38%       80ms  2.50%  runtime.memclr
(pprof)

参考: https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md 

默认状况下top命令会列出前10项内容。可是若是在top命令后面紧跟一个数字,那么其列出的项数就会与这个数字相同。

 

web

与gv命令相似,web命令也会用图形化的方式来显示概要文件。但不一样的是,web命令是在一个Web浏览器中显示它。若是你的Web浏览器已经启动,那么它的显示速度会很是快。若是想改变所使用的Web浏览器,能够在Linux下设置符号连接/etc/alternatives/gnome-www-browser或/etc/alternatives/x-www-browser,或在OS X下改变SVG文件的关联Finder。

mac 下 修改默认打开方式: 右键一个想处理的文件,按alt 键(lion)出现always open with,而后打开,整个过程当中, 先右键,而后一直按 alt, 一直到打开为止。

image

 

 

参考资料:

go tool pprof
https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md 

Go的pprof使用
http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html 

Profiling Go Programs
https://blog.golang.org/profiling-go-programs 

 

内存泄漏或消耗分析

关键代码

fm, err := os.OpenFile("./tmp/mem.out", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
    log.Fatal(err)
}
pprof.WriteHeapProfile(fm)
fm.Close()

在咱们须要生成当时的内存状况时,只须要执行上面代码便可。

触发的条件能够经过 http 的一个接口,或者退出时,或者接收到某个特殊信号,这些逻辑就须要本身实现了。

分析方法,跟之上CPU的分析方法一致。

image

 

参考资料:

[golang]内存不断增加bytes.makeSlice
http://studygolang.com/articles/2763

相关文章
相关标签/搜索