如何降低在Linux上运行的Go进程的占用空间?

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

How can I lower the footprint of a Go process running on Linux?

问题

我想在内存非常有限的设备上执行使用Go编写的“Hello world”程序。

在Linux上运行时,当前的内存占用似乎非常高(hello world的VM大小为64MB,VM数据为40MB)。

我该如何配置Go运行时环境以减少内存使用?

英文:

I want to execute a "Hello world" program written using Go on device with very limited memory.

When running it over Linux, the current memory footprint seems to be very high (64MB VM Size and 40MB VM Data for hello world).

How can I configure the Go runtime environment to use less memory?

答案1

得分: 3

请注意,带有“virtual”名称的内存使用指标是无用的,因为它们只是虚拟的。

Go语言的运行时(对于由gc工具链构建的二进制文件,gccgo可能使用自己的分配方法,我不能确定)在Linux上使用所谓的“arena分配器”,它在启动时告诉操作系统它想要“拥有”一个非常大的内存区域,操作系统确认了这一点,但实际上并没有分配任何内存(即没有物理内存页),只有当进程真正请求内存时才会进行真正的分配。

因此,唯一有意义的内存参数是RSS(Resident Set Size),它是映射到进程地址空间的物理内存量,即实际分配和拥有的内存,而不是虚拟统计信息。请参阅这个链接以获得一个很好的解释,并大致浏览一下这个链接

英文:

Note that memory usage indicators having "virtual" in their names are useless to analyze as they are, as stated, virtual.

Go's runtime (for binaries built by the gc toolchain, gccgo might use it own approach to allocation—I don't know for sure) on Linux uses the so-called "arena allocator" which at startup tells the OS it wants to "own" a memory region of a pretty huge size, the OS acknowledges this but no memory is really allocated (no physical memory pages, that is), and real allocation only happens when the process really requests the memory.

Due to this, the only sensible memory parameter to analyze is RSS—Resident Set Size, which is the amount of physical memory mapped to the process' address space—the memory it physically allocated and owns—as opposed to virtual stats. See this for a good explanation and skim through this in general.

huangapple
  • 本文由 发表于 2014年5月1日 17:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/23405510.html
匿名

发表评论

匿名网友

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

确定