为了账号安全,请及时绑定邮箱和手机立即绑定

memstat HeapInuse 和 TotalAlloc 需要解释一下

memstat HeapInuse 和 TotalAlloc 需要解释一下

Go
慕码人2483693 2022-11-08 14:36:45
我想转储 golang memstatpackage mainimport (    "fmt"    "github.com/dustin/go-humanize"    "runtime"    "time")func main() {    for {        var mem = new(runtime.MemStats)        runtime.ReadMemStats(mem)        fmt.Println("HeapInuse:", humanize.Bytes(mem.HeapInuse), "TotalAlloc:", humanize.Bytes(mem.TotalAlloc))        allocate()        time.Sleep(100 * time.Millisecond)    }}func allocate() {    //  0.25MB    _ = make([]byte, int((1<<20)*0.25))}当我转储内存信息时HeapInuse: 418 kB TotalAlloc: 154 kB ? why  HeapInuse bigger than TotalAllocHeapInuse: 688 kB TotalAlloc: 417 kBHeapInuse: 950 kB TotalAlloc: 680 kBHeapInuse: 1.2 MB TotalAlloc: 942 kBHeapInuse: 1.5 MB TotalAlloc: 1.2 MBHeapInuse: 1.8 MB TotalAlloc: 1.5 MBHeapInuse: 2.0 MB TotalAlloc: 1.7 MBHeapInuse: 2.3 MB TotalAlloc: 2.0 MBHeapInuse: 2.5 MB TotalAlloc: 2.3 MBHeapInuse: 2.8 MB TotalAlloc: 2.5 MBHeapInuse: 3.1 MB TotalAlloc: 2.8 MBHeapInuse: 3.3 MB TotalAlloc: 3.0 MBHeapInuse: 3.6 MB TotalAlloc: 3.3 MBHeapInuse: 3.9 MB TotalAlloc: 3.6 MBHeapInuse: 4.1 MB TotalAlloc: 3.8 MBHeapInuse: 4.4 MB TotalAlloc: 4.1 MBHeapInuse: 410 kB TotalAlloc: 4.4 MB我发现 HeapInuse 比 TotalAlloc 大?但我无法理解原因?我认为 totalAlloc 应该包括 HeapInuse。
查看完整描述

1 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

让我们阅读下面的官方评论:


总分配


// TotalAlloc is cumulative bytes allocated for heap objects.

//

// TotalAlloc increases as heap objects are allocated, but

// unlike Alloc and HeapAlloc, it does not decrease when

// objects are freed.

TotalAlloc uint64   

堆使用


// HeapInuse is bytes in in-use spans.

//

// In-use spans have at least one object in them. These spans

// can only be used for other objects of roughly the same

// size.

//

// HeapInuse minus HeapAlloc estimates the amount of memory

// that has been dedicated to particular size classes, but is

// not currently being used. This is an upper bound on

// fragmentation, but in general this memory can be reused

// efficiently.

HeapInuse uint64

HeapInuse 是分配的内存(来自操作系统),可能不被对象使用

TotalAlloc 是为堆对象分配的内存

让我们试试HeapAlloc


// HeapAlloc is bytes of allocated heap objects.

//

// "Allocated" heap objects include all reachable objects, as

// well as unreachable objects that the garbage collector has

// not yet freed. Specifically, HeapAlloc increases as heap

// objects are allocated and decreases as the heap is swept

// and unreachable objects are freed. Sweeping occurs

// incrementally between GC cycles, so these two processes

// occur simultaneously, and as a result HeapAlloc tends to

// change smoothly (in contrast with the sawtooth that is

// typical of stop-the-world garbage collectors).

HeapAlloc uint64

结果


HeapAlloc: 148 kB HeapInuse: 401 kB TotalAlloc: 148 kB

HeapAlloc: 412 kB HeapInuse: 664 kB TotalAlloc: 412 kB

HeapAlloc: 675 kB HeapInuse: 926 kB TotalAlloc: 675 kB

HeapAlloc: 937 kB HeapInuse: 1.2 MB TotalAlloc: 937 kB

HeapAlloc: 1.2 MB HeapInuse: 1.5 MB TotalAlloc: 1.2 MB

HeapAlloc: 1.5 MB HeapInuse: 1.7 MB TotalAlloc: 1.5 MB

HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 1.7 MB

HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 2.0 MB

HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 2.2 MB

HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 2.5 MB

HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 2.8 MB

HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 3.0 MB

HeapAlloc: 3.3 MB HeapInuse: 3.6 MB TotalAlloc: 3.3 MB

HeapAlloc: 3.6 MB HeapInuse: 3.8 MB TotalAlloc: 3.6 MB

HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 3.8 MB

HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 4.1 MB

HeapAlloc: 134 kB HeapInuse: 393 kB TotalAlloc: 4.4 MB

HeapAlloc: 398 kB HeapInuse: 664 kB TotalAlloc: 4.6 MB

HeapAlloc: 660 kB HeapInuse: 926 kB TotalAlloc: 4.9 MB

HeapAlloc: 923 kB HeapInuse: 1.2 MB TotalAlloc: 5.1 MB

HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 5.4 MB

HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 5.7 MB

HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 5.9 MB

HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 6.2 MB

HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 6.5 MB

HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 6.7 MB

HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 7.0 MB

HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 7.2 MB

HeapAlloc: 3.3 MB HeapInuse: 3.5 MB TotalAlloc: 7.5 MB

HeapAlloc: 3.5 MB HeapInuse: 3.8 MB TotalAlloc: 7.8 MB

HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 8.0 MB

HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 8.3 MB

HeapAlloc: 136 kB HeapInuse: 401 kB TotalAlloc: 8.6 MB

HeapAlloc: 400 kB HeapInuse: 664 kB TotalAlloc: 8.8 MB

HeapAlloc: 662 kB HeapInuse: 926 kB TotalAlloc: 9.1 MB

HeapAlloc: 924 kB HeapInuse: 1.2 MB TotalAlloc: 9.3 MB

HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 9.6 MB

HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 9.9 MB

HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 10 MB

HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 10 MB

HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 11 MB

HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 11 MB

HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 11 MB

HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 11 MB

HeapAlloc: 3.3 MB HeapInuse: 3.5 MB TotalAlloc: 12 MB

HeapAlloc: 3.5 MB HeapInuse: 3.8 MB TotalAlloc: 12 MB

HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 12 MB

HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 12 MB

HeapAlloc: 136 kB HeapInuse: 401 kB TotalAlloc: 13 MB

HeapAlloc: 400 kB HeapInuse: 664 kB TotalAlloc: 13 MB

HeapAlloc: 663 kB HeapInuse: 926 kB TotalAlloc: 13 MB

HeapAlloc: 925 kB HeapInuse: 1.2 MB TotalAlloc: 14 MB

HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 14 MB

HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 14 MB



查看完整回答
反对 回复 2022-11-08
  • 1 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信