2 回答
TA贡献2036条经验 获得超8个赞
我能够找到 gccgo 在哪里要求这么多内存。它在 mallocinit 函数的 libgo/go/runtime/malloc.go 文件中:
// If we fail to allocate, try again with a smaller arena.
// This is necessary on Android L where we share a process
// with ART, which reserves virtual memory aggressively.
// In the worst case, fall back to a 0-sized initial arena,
// in the hope that subsequent reservations will succeed.
arenaSizes := [...]uintptr{
512 << 20,
256 << 20,
128 << 20,
0,
}
for _, arenaSize := range &arenaSizes {
// SysReserve treats the address we ask for, end, as a hint,
// not as an absolute requirement. If we ask for the end
// of the data segment but the operating system requires
// a little more space before we can start allocating, it will
// give out a slightly higher pointer. Except QEMU, which
// is buggy, as usual: it won't adjust the pointer upward.
// So adjust it upward a little bit ourselves: 1/4 MB to get
// away from the running binary image and then round up
// to a MB boundary.
p = round(getEnd()+(1<<18), 1<<20)
pSize = bitmapSize + spansSize + arenaSize + _PageSize
if p <= procBrk && procBrk < p+pSize {
// Move the start above the brk,
// leaving some room for future brk
// expansion.
p = round(procBrk+(1<<20), 1<<20)
}
p = uintptr(sysReserve(unsafe.Pointer(p), pSize, &reserved))
if p != 0 {
break
}
}
if p == 0 {
throw("runtime: cannot reserve arena virtual address space")
}
有趣的是,如果较大的竞技场失败,它会退回到较小的竞技场。因此,限制 go 可执行文件可用的虚拟内存实际上会限制它成功分配的数量。
我能够使用ulimit -v 327680将虚拟内存限制为较小的数字:
VmPeak: 300772 kB
VmSize: 300772 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 5712 kB
VmRSS: 5712 kB
VmData: 296276 kB
VmStk: 132 kB
VmExe: 2936 kB
VmLib: 0 kB
VmPTE: 56 kB
VmPMD: 0 kB
VmSwap: 0 kB
这些仍然是很大的数字,但是 gccgo 可执行文件可以达到的最好结果。所以问题的答案是,是的,你可以减少 gccgo 编译的可执行文件的 VmData,但你真的不应该为此担心。(在 64 位机器上,gccgo 尝试分配 512 GB。)
TA贡献1793条经验 获得超6个赞
可能的原因是您将库链接到代码中。我的猜测是,如果您要显式链接到静态库,那么您将能够获得更小的逻辑地址空间,以便将最少的内容添加到您的可执行文件中。无论如何,拥有较大的逻辑地址空间的危害最小。
- 2 回答
- 0 关注
- 104 浏览
添加回答
举报