Skip to content

Commit c5dea8f

Browse files
committed
runtime: remove memstats.heap_idle
This statistic is updated in many places but for MemStats may be computed from existing statistics. Specifically by definition heap_idle = heap_sys - heap_inuse since heap_sys is all memory allocated from the OS for use in the heap minus memory used for non-heap purposes. heap_idle is almost the same (since it explicitly includes memory that *could* be used for non-heap purposes) but also doesn't include memory that's actually used to hold heap objects. Although it has some utility as a sanity check, it complicates accounting and we want fewer, orthogonal statistics for upcoming metrics changes, so just drop it. Change-Id: I40af54a38e335f43249f6e218f35088bfd4380d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/246974 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent ad863ba commit c5dea8f

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/runtime/heapdump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func dumpmemstats() {
552552
dumpint(memstats.nfree)
553553
dumpint(memstats.heap_alloc)
554554
dumpint(memstats.heap_sys.load())
555-
dumpint(memstats.heap_idle)
555+
dumpint(memstats.heap_sys.load() - memstats.heap_inuse)
556556
dumpint(memstats.heap_inuse)
557557
dumpint(memstats.heap_released)
558558
dumpint(memstats.heap_objects)

src/runtime/mheap.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,6 @@ HaveSpan:
12391239
// Manually managed memory doesn't count toward heap_sys.
12401240
memstats.heap_sys.add(-int64(nbytes))
12411241
}
1242-
atomic.Xadd64(&memstats.heap_idle, -int64(nbytes))
12431242

12441243
// Publish the span in various locations.
12451244

@@ -1317,7 +1316,6 @@ func (h *mheap) grow(npage uintptr) bool {
13171316
// size which is always > physPageSize, so its safe to
13181317
// just add directly to heap_released.
13191318
atomic.Xadd64(&memstats.heap_released, int64(asize))
1320-
atomic.Xadd64(&memstats.heap_idle, int64(asize))
13211319

13221320
// Recalculate nBase.
13231321
// We know this won't overflow, because sysAlloc returned
@@ -1417,7 +1415,6 @@ func (h *mheap) freeSpanLocked(s *mspan, typ spanAllocType) {
14171415
// Manually managed memory doesn't count toward heap_sys, so add it back.
14181416
memstats.heap_sys.add(int64(nbytes))
14191417
}
1420-
atomic.Xadd64(&memstats.heap_idle, int64(nbytes))
14211418

14221419
// Mark the space as free.
14231420
h.pages.free(s.base(), s.npages)

src/runtime/mstats.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ type mstats struct {
3434
// in manually-managed spans.
3535
heap_alloc uint64 // bytes allocated and not yet freed (same as alloc above)
3636
heap_sys sysMemStat // virtual address space obtained from system for GC'd heap
37-
heap_idle uint64 // bytes in idle spans
3837
heap_inuse uint64 // bytes in mSpanInUse spans
3938
heap_released uint64 // bytes released to the os
4039

@@ -461,7 +460,23 @@ func readmemstats_m(stats *MemStats) {
461460
stats.Frees = memstats.nfree
462461
stats.HeapAlloc = memstats.heap_alloc
463462
stats.HeapSys = memstats.heap_sys.load()
464-
stats.HeapIdle = memstats.heap_idle
463+
// By definition, HeapIdle is memory that was mapped
464+
// for the heap but is not currently used to hold heap
465+
// objects. It also specifically is memory that can be
466+
// used for other purposes, like stacks, but this memory
467+
// is subtracted out of HeapSys before it makes that
468+
// transition. Put another way:
469+
//
470+
// heap_sys = bytes allocated from the OS for the heap - bytes ultimately used for non-heap purposes
471+
// heap_idle = bytes allocated from the OS for the heap - bytes ultimately used for any purpose
472+
//
473+
// or
474+
//
475+
// heap_sys = sys - stacks_inuse - gcWorkBufInUse - gcProgPtrScalarBitsInUse
476+
// heap_idle = sys - stacks_inuse - gcWorkBufInUse - gcProgPtrScalarBitsInUse - heap_inuse
477+
//
478+
// => heap_idle = heap_sys - heap_inuse
479+
stats.HeapIdle = memstats.heap_sys.load() - memstats.heap_inuse
465480
stats.HeapInuse = memstats.heap_inuse
466481
stats.HeapReleased = memstats.heap_released
467482
stats.HeapObjects = memstats.heap_objects

0 commit comments

Comments
 (0)