Skip to content

pprof

使用

import (_ "net/http/pprof")

func AddPprofEndpointsToGin(ginEngine *gin.Engine) {
    prefixRouter := ginEngine.RouterGroup.Group("/debug/pprof")
    prefixRouter.GET("/", gin.WrapF(pprof.Index))
    prefixRouter.GET("/cmdline", gin.WrapF(pprof.Cmdline))
    prefixRouter.GET("/profile", gin.WrapF(pprof.Profile))
    prefixRouter.POST("/symbol", gin.WrapF(pprof.Symbol))
    prefixRouter.GET("/symbol", gin.WrapF(pprof.Symbol))
    prefixRouter.GET("/trace", gin.WrapF(pprof.Trace))
    prefixRouter.GET("/allocs", gin.WrapH(pprof.Handler("allocs")))
    prefixRouter.GET("/block", gin.WrapH(pprof.Handler("block")))
    prefixRouter.GET("/goroutine", gin.WrapH(pprof.Handler("goroutine")))
    prefixRouter.GET("/heap", gin.WrapH(pprof.Handler("heap")))
    prefixRouter.GET("/mutex", gin.WrapH(pprof.Handler("mutex")))
    prefixRouter.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate")))
}

导出

curl -o profile.out http://localhost:6060/debug/pprof/profile
curl -o heap.out http://localhost:13579/debug/pprof/heap

分析

go tool pprof xxx.out
> top xx
> list xx

go tool pprof -http=:8080 xxx.out

One is around looking at the current allocations (bytes or object count), called inuse. The other is looking at all the allocated bytes or object count throughout the run-time of the program, called alloc

refs

golang pprof 实战 | Wolfogre's Blog

实用go pprof使用指南 - 知乎

How we tracked down (what seemed like) a memory leak in one of our Go microservices - Detectify Blog