Go运行时有很多与堆和堆栈相关的不同变量,一些堆栈号是堆号的一部分,导致混乱(对我来说).例如,在此链接中.它说
// Stack numbers are part of the heap numbers, separate those out for user consumption
stats.StackSys = stats.StackInuse
stats.HeapInuse -= stats.StackInuse
stats.HeapSys -= stats.StackInuse
Run Code Online (Sandbox Code Playgroud)
在运行时文档(下面的摘录)中,它提供了7个不同的堆相关字段(即memstat结构的字段),但没有明确说明哪些包含堆栈,类似地,哪些堆栈字段包含在堆中,以及它们与总分配.
这是一个问题,因为我想比较堆与堆栈,但我不想选择包含堆栈的堆变量(显然).
问题1).总分配字段是包含堆,堆栈还是两者?2)哪些堆字段不包含数字堆栈?3)哪些堆字段包含堆栈的数字?4)哪些堆栈字段不包含堆的数字?
Alloc uint64 // bytes allocated and still in use
TotalAlloc uint64 // bytes allocated (even if freed)
Sys uint64 // bytes obtained from system (sum of XxxSys below)
Lookups uint64 // number of pointer lookups
Mallocs uint64 // number of mallocs
Frees uint64 // number of frees
// Main allocation heap statistics.
HeapAlloc uint64 // bytes allocated and still in use
HeapSys uint64 // bytes obtained from system
HeapIdle uint64 // bytes in idle spans
HeapInuse uint64 // bytes in non-idle span
HeapReleased uint64 // bytes released to the OS
HeapObjects uint64 // total number of allocated objects
// Low-level fixed-size structure allocator statistics.
// Inuse is bytes used now.
// Sys is bytes obtained from system.
StackInuse uint64 // bytes used by stack allocator
StackSys uint64
Run Code Online (Sandbox Code Playgroud)
这些问题有点难以回答,因为goroutine堆栈是从堆中分配的.Go没有C中存在的堆栈和堆之间的明确分离.
总分配字段是包含堆,堆栈还是两者?
MemStats结构的TotalAlloc字段包括Go运行时从操作系统为Go堆请求的所有内存.它不包括为goroutine堆栈分配的内存.最初我认为它确实如此,但我错了.对困惑感到抱歉.我希望这个答案更准确.
(确切地说,我应该提到在使用cgo的程序中,每个线程(不是goroutine - 通常比线程更多的goroutine)将具有由OS分配的堆栈;该堆栈不是由Go运行时分配的,而是不计入TotalAlloc.它仅用于cgo调用.)
哪些堆字段不包含数字堆栈? 哪些堆字段包含堆栈的数字?
这些字段包括goroutine堆栈的数字:HeapIdle,HeapReleased.
这些字段不包括goroutine堆栈的数字:HeapAlloc,HeapInUse,HeapObjects.
HeapSys字段不包括当前活动的goroutine堆栈使用的内存,但包括曾经使用但随后被释放的goroutine堆栈的内存.
哪些堆栈字段不包含堆的数字?
我不知道如何以有道理的方式回答这个问题.堆栈字段特别报告有关goroutine堆栈的信息.