go笔记-pprof使用
pprof如何进行采样:https://studygolang.com/articles/11873
[译] 我是如何在大型代码库上使用 pprof 调查 Go 中的内存泄漏 https://juejin.cn/post/6844903848083980301
go tool pprof http://localhost:6060/debug/pprof/profile
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/block
go tool pprof http://localhost:6060/debug/pprof/mutex
- cpu(CPU Profiling):
$HOST/debug/pprof/profile
,默认进行 30s 的 CPU Profiling,得到一个分析用的 profile 文件 - block(Block Profiling):
$HOST/debug/pprof/block
,查看导致阻塞同步的堆栈跟踪 - goroutine:
$HOST/debug/pprof/goroutine
,查看当前所有运行的 goroutines 堆栈跟踪 - heap(Memory Profiling):
$HOST/debug/pprof/heap
,查看活动对象的内存分配情况 - mutex(Mutex Profiling):
$HOST/debug/pprof/mutex
,查看导致互斥锁的竞争持有者的堆栈跟踪 - threadcreate:
$HOST/debug/pprof/threadcreate
,查看创建新OS线程的堆栈跟踪
分析heap可生成以下内容
(pprof) top Showing nodes accounting for 330.04MB, 93.73% of 352.11MB total Dropped 19 nodes (cum <= 1.76MB) Showing top 10 nodes out of 56 flat flat% sum% cum cum% 142.02MB 40.33% 40.33% 142.02MB 40.33% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/membuffers/go.(*InternalMessage).lazyCalcOffsets 28MB 7.95% 48.29% 28MB 7.95% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.TransactionsBlockProofReader (inline) 26.51MB 7.53% 55.81% 39.01MB 11.08% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.(*ResultsBlockHeaderBuilder).Build 25.51MB 7.24% 63.06% 32.51MB 9.23% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.(*ResultsBlockProofBuilder).Build 23MB 6.53% 69.59% 23MB 6.53% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.ResultsBlockHeaderReader (inline) 20.50MB 5.82% 75.41% 20.50MB 5.82% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.TransactionsBlockMetadataReader (inline) 20MB 5.68% 81.09% 20MB 5.68% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.TransactionsBlockHeaderReader (inline) 16MB 4.54% 85.64% 24MB 6.82% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.(*TransactionsBlockHeaderBuilder).Build 14.50MB 4.12% 89.76% 122.51MB 34.79% github.com/orbs-network/orbs-network-go/services/gossip/codec.DecodeBlockPairs 14MB 3.98% 93.73% 14MB 3.98% github.com/orbs-network/orbs-network-go/vendor/github.com/orbs-network/orbs-spec/types/go/protocol.ResultsBlockProofReader (inline)
我们可以看到关于Dropped Nodes
的一系列数据,这意味着它们被过滤掉了。一个节点或树中的一个“节点”就是一整个对象。丢弃节点有利于我们更快的找到问题,但有时它可能会隐藏内存问题产生的根本原因。我们继续看一个例子。
如果要该画像文件的所有数据,请在运行 pprof 时添加-nodefraction=0
选项,或在交互命令行中键入nodefraction=0
。
在输出列表中,我们可以看到两个值,flat
和cum
。
flat
表示堆栈中当前层函数的内存cum
表示堆栈中直到当前层函数所累积的内存
仅仅这个信息有时可以帮助我们了解是否存在问题。例如,一个函数负责分配了大量内存但没有保留内存的情况。这意味着某些其他对象指向该内存并维护其分配,这说明我们可能存在系统设计的问题或bug