为什么在golang堆剖析中的“Total MB”比top命令中的“RES”要小?

huangapple go评论87阅读模式
英文:

Why 'Total MB' in golang heap profile is less than 'RES' in top?

问题

我有一个用Go语言编写的服务,在运行时占用6-7G的内存(在top命令中的RES)。所以我使用了pprof工具来找出问题所在。

go tool pprof --pdf http://<service>/debug/pprof/heap > heap_prof.pdf

但是结果中只有大约1-2G的内存(pdf中的'Total MB')。剩下的内存在哪里?

我还尝试使用GOGC=off来对我的服务进行性能分析,结果'Total MB'与top命令中的'RES'完全相同。看起来内存已经被垃圾回收了,但没有被归还给内核,所以无法进行性能分析。

有什么想法吗?

附注:我在1.0.3和1.1rc3版本中都进行了测试。

英文:

I have a service written in go that takes 6-7G memory at runtime (RES in top). So I used the pprof tool trying to figure out where the problem is.

go tool pprof --pdf http://&lt;service&gt;/debug/pprof/heap &gt; heap_prof.pdf

But there are only about 1-2G memory in result ('Total MB' in pdf). Where's the rest ?

And I've tried profile my service with GOGC=off, as a result the 'Total MB' is exactly the same as 'RES' in top. It seems that memory is GCed but haven't been return to kernel won't be profiled.

Any idea?

P.S, I've tested in both 1.0.3 and 1.1rc3.

答案1

得分: 9

这是因为Go目前不会将被垃圾回收的对象的内存返还给操作系统,准确地说,只有小于预定义限制(32KB)的对象才会被返还。相反,内存会被缓存以加快未来的分配速度Go:malloc。此外,似乎这个问题将在未来得到解决TODO

编辑:
新的垃圾回收行为:如果内存一段时间没有被使用(大约5分钟),运行时会建议内核从未使用的虚拟范围中删除物理映射。可以通过调用runtime.FreeOSMemory()来强制执行此过程。

英文:

This is because Go currently does not give memory of GC-ed objects back to the operating system, to be precise, only for objects smaller then predefined limit (32KB). Instead memory is cached to speed up future allocations Go:malloc. Also, it seems that this is going to be fixed in the future TODO.

Edit:
New GC behavior: If the memory is not used for a while (about 5 min), runtime will advise the kernel to remove the physical mappings from the unused virtual ranges. This process can be forced by calling runtime.FreeOSMemory()

huangapple
  • 本文由 发表于 2013年5月13日 14:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/16516189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定