如何分析golang记忆? [英] How to analyze golang memory?

查看:98
本文介绍了如何分析golang记忆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个golang程序,该程序在运行时使用1.2GB的内存.

I wrote a golang program, that uses 1.2GB of memory at runtime.

调用go tool pprof http://10.10.58.118:8601/debug/pprof/heap将导致仅323.4MB堆使用的转储.

Calling go tool pprof http://10.10.58.118:8601/debug/pprof/heap results in a dump with only 323.4MB heap usage.

  • 其余的内存使用情况如何?
  • 有没有更好的工具来解释golang运行时内存?

使用gcvis我得到了:

..以及此堆表单配置文件:

.. and this heap form profile:

这是我的代码: https://github.com/sharewind/push -server/blob/v3/broker

推荐答案

堆配置文件显示活动内存,运行时认为该内存正在被go程序使用(即:垃圾收集器未收集).当GC确实收集内存时,配置文件会缩小,但是没有内存返回给系统.您将来的分配将尝试在请求系统提供更多信息之前,使用先前收集的对象池中的内存.

The heap profile shows active memory, memory the runtime believes is in use by the go program (ie: hasn't been collected by the garbage collector). When the GC does collect memory the profile shrinks, but no memory is returned to the system. Your future allocations will try to use memory from the pool of previously collected objects before asking the system for more.

从外部开始,这意味着程序的内存使用量将增加或保持不变.外部系统显示为程序的常驻大小"是指程序是保留正在使用的go值还是已收集的go值,这是分配给您程序的RAM字节数.

From the outside, this means that your program's memory use will either be increasing, or staying level. What the outside system presents as the "Resident Size" of your program is the number of bytes of RAM is assigned to your program whether it's holding in-use go values or collected ones.

这两个数字经常有很大差异的原因是:

The reason why these two numbers are often quite different are because:

  1. GC收集内存对程序的外部视图没有影响
  2. 内存碎片
  3. GC仅在以下情况下运行:正在使用的内存是前一个GC之后的使用内存的两倍(默认情况下,请参见: http://golang.org/pkg/runtime/#pkg-overview )

如果要准确了解Go如何查看内存,可以使用运行时.ReadMemStats调用: http://golang .org/pkg/runtime/#ReadMemStats

If you want an accurate breakdown of how Go sees the memory you can use the runtime.ReadMemStats call: http://golang.org/pkg/runtime/#ReadMemStats

或者,由于您使用的是基于Web的分析,因此可以通过以下浏览器访问分析数据:http://10.10.58.118:8601/debug/pprof/,单击堆"链接将向您显示堆概要文件的调试视图,该概要文件的打印输出为底部的runtime.MemStats结构.

Alternatively, since you are using web-based profiling if you can access the profiling data through your browser at: http://10.10.58.118:8601/debug/pprof/ , clicking the heap link will show you the debugging view of the heap profile, which has a printout of a runtime.MemStats structure at the bottom.

runtime.MemStats文档( http://golang.org/pkg/runtime/#MemStats )具有所有字段的解释,但有趣的讨论是:

The runtime.MemStats documentation (http://golang.org/pkg/runtime/#MemStats) has the explanation of all the fields, but the interesting ones for this discussion are:

  • HeapAlloc:本质上是探查器为您提供的(活动堆内存)
  • Alloc:与HeapAlloc相似,但全部用于托管内存
  • Sys:操作系统请求的内存总量(地址空间)

Sys和OS报告的内容之间仍然存在差异,因为Go询问系统的内容以及OS给它的内容并不总是相同的.此外,go不会跟踪CGO/syscall(例如:malloc/mmap)内存.

There will still be discrepancies between Sys, and what the OS reports because what Go asks of the system, and what the OS gives it are not always the same. Also CGO / syscall (eg: malloc / mmap) memory is not tracked by go.

这篇关于如何分析golang记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆