memstat HeapInuse and TotalAlloc need explain

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

memstat HeapInuse and TotalAlloc need explain

问题

我想要转储Golang的内存统计信息。

package main

import (
	"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比TotalAlloc大?但我不明白原因。我认为TotalAlloc应该包括HeapInuse。

英文:

i want dump golang memstat

package main

import (
	&quot;fmt&quot;
	&quot;github.com/dustin/go-humanize&quot;
	&quot;runtime&quot;
	&quot;time&quot;
)

func main() {
	for {
		var mem = new(runtime.MemStats)
		runtime.ReadMemStats(mem)
		fmt.Println(&quot;HeapInuse:&quot;, humanize.Bytes(mem.HeapInuse), &quot;TotalAlloc:&quot;, humanize.Bytes(mem.TotalAlloc))

		allocate()
		time.Sleep(100 * time.Millisecond)
	}

}

func allocate() {
	//  0.25MB
	_ = make([]byte, int((1&lt;&lt;20)*0.25))
}

when i dump the mem info

HeapInuse: 418 kB TotalAlloc: 154 kB ? why  HeapInuse bigger than TotalAlloc
HeapInuse: 688 kB TotalAlloc: 417 kB
HeapInuse: 950 kB TotalAlloc: 680 kB
HeapInuse: 1.2 MB TotalAlloc: 942 kB
HeapInuse: 1.5 MB TotalAlloc: 1.2 MB
HeapInuse: 1.8 MB TotalAlloc: 1.5 MB
HeapInuse: 2.0 MB TotalAlloc: 1.7 MB
HeapInuse: 2.3 MB TotalAlloc: 2.0 MB
HeapInuse: 2.5 MB TotalAlloc: 2.3 MB
HeapInuse: 2.8 MB TotalAlloc: 2.5 MB
HeapInuse: 3.1 MB TotalAlloc: 2.8 MB
HeapInuse: 3.3 MB TotalAlloc: 3.0 MB
HeapInuse: 3.6 MB TotalAlloc: 3.3 MB
HeapInuse: 3.9 MB TotalAlloc: 3.6 MB
HeapInuse: 4.1 MB TotalAlloc: 3.8 MB
HeapInuse: 4.4 MB TotalAlloc: 4.1 MB
HeapInuse: 410 kB TotalAlloc: 4.4 MB

i found HeapInuse bigger than TotalAlloc? but i cannt understand reason ? i think the totalAlloc should include HeapInuse.

答案1

得分: 2

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

TotalAlloc

// TotalAlloc 是分配给堆对象的累积字节数。
//
// TotalAlloc 在分配堆对象时增加,但与 Alloc 和 HeapAlloc 不同,
// 当对象被释放时,它不会减少。
TotalAlloc uint64

HeapInuse

// HeapInuse 是正在使用的字节数。
//
// 正在使用的 span 至少包含一个对象。这些 span
// 只能用于大致相同大小的其他对象。
//
// HeapInuse 减去 HeapAlloc 估计了已经分配给特定大小类别的内存量,
// 但目前未被使用。这是碎片化的上限,但通常可以高效地重用这些内存。
HeapInuse uint64

  • HeapInuse 是分配给堆的内存(来自操作系统),可能未被对象使用
  • TotalAlloc 是为堆对象分配的内存

让我们尝试 HeapAlloc

// HeapAlloc 是分配的堆对象的字节数。
//
// “已分配”的堆对象包括所有可达对象,以及垃圾收集器尚未释放的不可达对象。
// 具体而言,HeapAlloc 在分配堆对象时增加,在堆被扫描并释放不可达对象时减少。
// 扫描在 GC 循环之间逐步进行,因此这两个过程同时进行,
// 结果是 HeapAlloc 倾向于平滑变化(与停止-全局垃圾收集器的锯齿形相反)。
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

英文:

Let's read the official comments below:

TotalAlloc

// 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

// 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 is memory allocated (from the OS), may not be used by objects
  • TotalAlloc is memory allocated for heap objects

Let's try HeapAlloc

// HeapAlloc is bytes of allocated heap objects.
//
// &quot;Allocated&quot; 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

Result

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

huangapple
  • 本文由 发表于 2022年2月9日 14:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/71045082.html
匿名

发表评论

匿名网友

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

确定