我有一个 Go 程序,可以计算内存中的大型相关矩阵。为此,我设置了一个包含 3 个 goroutine 的管道,其中第一个读取文件,第二个计算相关矩阵,最后一个将结果存储到磁盘。
问题是,当我运行程序时,Go 运行时分配了约 17GB 的内存,而矩阵只占用了约 2-3GB。Usingruntime.ReadMemStats
显示该程序正在使用 ~17GB(并通过使用 htop 验证),但pprof
仅报告大约 ~2.3GB。
如果我在通过管道运行一个文件后查看 mem 统计信息:
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
fmt.Printf("Total alloc: %d GB\n", mem.Alloc/1000/1000/1000)
Run Code Online (Sandbox Code Playgroud)
这显示了程序的总分配:
Total alloc: 17 GB
Run Code Online (Sandbox Code Playgroud)
但是,如果我运行,go tool pprof mem.prof
我会得到以下结果:
(pprof) top5
Showing nodes accounting for 2.21GB, 100% of 2.21GB total
Showing top 5 nodes out of 9
flat flat% sum% cum cum%
1.20GB 54.07% 54.07% 1.20GB 54.07% dataset.(*Dataset).CalcCorrelationMatrix
1.02GB 45.93% 100% 1.02GB 45.93% bytes.makeSlice
0 0% …
Run Code Online (Sandbox Code Playgroud)