【博客544】golang pprof性能调试:寻找memory瓶颈
创始人
2024-02-20 10:12:07
0

golang pprof性能调试:寻找memory瓶颈

1、前置

pprof的使用与输出列解析看姐妹篇:golang pprof性能调试:寻找cpu瓶颈

2、引入pprof到程序中,以调试memory瓶颈

给程序加入:

import _ "net/http/pprof"go func() {http.ListenAndServe("0.0.0.0:9999", nil)
}()

示例:随机拼接字符串

package mainimport ("math/rand""net/http"_ "net/http/pprof"
)const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"func randomString(n int) string {b := make([]byte, n)for i := range b {b[i] = letterBytes[rand.Intn(len(letterBytes))]}return string(b)
}func concat(n int) string {s := ""for i := 0; i < n; i++ {s += randomString(n)}return s
}func main() {go func() {http.ListenAndServe("0.0.0.0:9999", nil)}()for {concat(100)}
}

3、pprof命令行交互式查看程序最消耗memory的地方

1、启动上述程序:

go run main.go

2、访问pprof暴露的memory debug api接口,收集30s内的memory数据:

go tool pprof http://localhost:9999/debug/pprof/heap\?seconds\=30

3、在交互命令中,使用top,list来查看最消耗memory的地方

go tool pprof http://localhost:9999/debug/pprof/heap\?seconds\=30
Fetching profile over HTTP from http://localhost:9999/debug/pprof/heap?seconds=30
Saved profile in /Users/zejia.lu/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz
Type: inuse_space
Time: Nov 27, 2022 at 12:40pm (CST)
Duration: 30s, Total samples = 1MB
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for -1026.68kB, 100% of 1026.68kB totalflat  flat%   sum%        cum   cum%514.63kB 50.13% 50.13% -1026.68kB   100%  main.concat512.05kB 49.87%   100%  -512.05kB 49.87%  main.randomString0     0%   100% -1026.68kB   100%  main.main0     0%   100% -1026.68kB   100%  runtime.main
(pprof) list main.randomString
Total: 1MB
ROUTINE ======================== main.randomString in /Users/zejia.lu/memory_pprof/main.go512.05kB  512.05kB (flat, cum) 49.87% of Total.          .     11:func randomString(n int) string {.          .     12:	b := make([]byte, n).          .     13:	for i := range b {.          .     14:		b[i] = letterBytes[rand.Intn(len(letterBytes))].          .     15:	}512.05kB  512.05kB     16:	return string(b).          .     17:}.          .     18:.          .     19:func concat(n int) string {.          .     20:	s := "".          .     21:	for i := 0; i < n; i++ {

4、pprof ui方式查看程序最消耗memory的地方

1、调试时指定通过ui方式查看,ui地址为9900端口,要调试的服务暴露的pprof端口为9999:

go tool pprof -http=":9900" http://localhost:9999/debug/pprof/heap  -inuse_objects

2、访问9900端口查看ui界面进行可视化调试:

查看最消耗memory的地方:可以看到我们的concat逻辑函数就是memoory高消耗的地方
在这里插入图片描述
在这里插入图片描述

5、pprof ui方式查看memory消耗拓扑图

在这里插入图片描述

6、pprof ui方式查看memory消耗火焰图

在这里插入图片描述
在这里插入图片描述

7、查看其它类型的memroy,上述例子查看的是:inuse_space

inuse_space — 已分配但尚未释放的内存空间
inuse_objects——已分配但尚未释放的对象数量
alloc_space — 分配的内存总量(已释放的也会统计)
alloc_objects — 分配的对象总数(无论是否释放)

8、查看所有曾经申请过的内存总量

go tool pprof -http=":9900" http://localhost:9999/debug/pprof/allocs

结果:
在这里插入图片描述

9、如何调试其它方面的内容:

查看程序暴露的/debug/pprof/ api

http://127.0.0.1:9999/debug/pprof/

查看有哪些方面的调试内容:
在这里插入图片描述

常用调试示例:

# 获取30秒的CPU profiling
curl -o cpu.bin http://localhost:6060/debug/pprof/profile# 获取5秒的执行跟踪(有一定性能影响)
curl -o trace.bin http://localhost:6060/debug/pprof/trace?seconds=5# 获取内存使用的profile
curl -o heap.bin http://localhost:6060/debug/pprof/heap# 获取正在运行的goroutines列表
curl -o goroutines.txt http://localhost:6060/debug/pprof/goroutine?debug=2# 获取被blocking的goroutine列表
curl -o goroutines-blocking.txt http://localhost:6060/debug/pprof/block

如何分析火焰图

火焰图的调用顺序从下到上,每个方块代表一个函数,它上面一层表示这个函数会调用哪些函数,方块的大小代表了占用 CPU 使用时长长短。

相关内容

热门资讯

20州因签证问题起诉联邦政府,... 【环球时报特约记者 任重】据美国《新闻周刊》13日报道,当地时间12日,美国20个州起诉联邦政府,反...
水晶宫主帅格拉斯纳:0比3失利... 在12月15日的英超联赛中,水晶宫主场迎战曼城,最终以0比3的比分落败。这场比赛不仅对水晶宫来说是一...
明年将根据形势出台实施增量政策 ● 本报记者 熊彦莎 中央财办分管日常工作的副主任、中央农办主任韩文秀12月13日在中国国际经济交流...
“何晴告别仪式”将以家人送别的... 12月13日,著名演员何晴在北京安然离世,享年61岁。 记者从98版《水浒传》制片主任汪瑞处获悉,根...
外籍男子携带多件文物出境被查!... 12月14日,南都N视频记者从“海关发布”公众号获悉,近日,西九龙站海关查获出境旅客携带11件/套古...
21场0球!哈曼:利物浦花1.... 在利物浦2-0战胜布莱顿的赛前,前利物浦球员哈曼在德国天空体育上对新加盟的维尔茨发表了颇具争议的看法...
公益诉讼保障快递小哥出行安全 本报讯(记者 徐慧瑶)“师傅,您的快递三轮车雨刷器、后视镜都完好吗?车辆有没有定期检修……”近日,顺...
俄罗斯免签政策落地:解锁跨境新... 当俄罗斯对中国居民免签的政策正式落地,不仅为普通民众打开了“说走就走”的跨境出行大门,更在经济、人文...
原创 从... 近年来,随着全球地缘政治的变动与经济重心的转移,“东升西降”的趋势愈发明显。或许在不久的将来,这一局...
4斤黄金凤冠被男童碰坏!谁该担... 千万网红张凯毅的4斤重黄金凤冠,在免费展览中被男童触碰掉落损坏,这顶由其老公亲手打造、意义非凡的展品...