53
53
nextAlloc gcBlock // the next block that should be tried by the allocator
54
54
endBlock gcBlock // the block just past the end of the available space
55
55
gcTotalAlloc uint64 // total number of bytes allocated
56
+ gcTotalBlocks uint64 // total number of allocated blocks
56
57
gcMallocs uint64 // total number of allocations
57
58
gcFrees uint64 // total number of objects freed
59
+ gcFreedBlocks uint64 // total number of freed blocks
58
60
)
59
61
60
62
// zeroSizedAlloc is just a sentinel that gets returned when allocating 0 bytes.
@@ -285,6 +287,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
285
287
gcMallocs ++
286
288
287
289
neededBlocks := (size + (bytesPerBlock - 1 )) / bytesPerBlock
290
+ gcTotalBlocks += uint64 (neededBlocks )
288
291
289
292
// Continue looping until a run of free blocks has been found that fits the
290
293
// requested size.
@@ -619,20 +622,21 @@ func markRoot(addr, root uintptr) {
619
622
// It returns how many bytes are free in the heap after the sweep.
620
623
func sweep () (freeBytes uintptr ) {
621
624
freeCurrentObject := false
625
+ var freed uint64
622
626
for block := gcBlock (0 ); block < endBlock ; block ++ {
623
627
switch block .state () {
624
628
case blockStateHead :
625
629
// Unmarked head. Free it, including all tail blocks following it.
626
630
block .markFree ()
627
631
freeCurrentObject = true
628
632
gcFrees ++
629
- freeBytes += bytesPerBlock
633
+ freed ++
630
634
case blockStateTail :
631
635
if freeCurrentObject {
632
636
// This is a tail object following an unmarked head.
633
637
// Free it now.
634
638
block .markFree ()
635
- freeBytes += bytesPerBlock
639
+ freed ++
636
640
}
637
641
case blockStateMark :
638
642
// This is a marked object. The next tail blocks must not be freed,
@@ -644,6 +648,8 @@ func sweep() (freeBytes uintptr) {
644
648
freeBytes += bytesPerBlock
645
649
}
646
650
}
651
+ gcFreedBlocks += freed
652
+ freeBytes += uintptr (freed ) * bytesPerBlock
647
653
return
648
654
}
649
655
@@ -690,6 +696,8 @@ func ReadMemStats(m *MemStats) {
690
696
m .Mallocs = gcMallocs
691
697
m .Frees = gcFrees
692
698
m .Sys = uint64 (heapEnd - heapStart )
699
+ m .HeapAlloc = (gcTotalBlocks - gcFreedBlocks ) * uint64 (bytesPerBlock )
700
+ m .Alloc = m .HeapAlloc
693
701
}
694
702
695
703
func SetFinalizer (obj interface {}, finalizer interface {}) {
0 commit comments