5353 nextAlloc gcBlock // the next block that should be tried by the allocator
5454 endBlock gcBlock // the block just past the end of the available space
5555 gcTotalAlloc uint64 // total number of bytes allocated
56+ gcTotalBlocks uint64 // total number of allocated blocks
5657 gcMallocs uint64 // total number of allocations
5758 gcFrees uint64 // total number of objects freed
59+ gcFreedBlocks uint64 // total number of freed blocks
5860)
5961
6062// 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 {
285287 gcMallocs ++
286288
287289 neededBlocks := (size + (bytesPerBlock - 1 )) / bytesPerBlock
290+ gcTotalBlocks += uint64 (neededBlocks )
288291
289292 // Continue looping until a run of free blocks has been found that fits the
290293 // requested size.
@@ -619,20 +622,21 @@ func markRoot(addr, root uintptr) {
619622// It returns how many bytes are free in the heap after the sweep.
620623func sweep () (freeBytes uintptr ) {
621624 freeCurrentObject := false
625+ var freed uint64
622626 for block := gcBlock (0 ); block < endBlock ; block ++ {
623627 switch block .state () {
624628 case blockStateHead :
625629 // Unmarked head. Free it, including all tail blocks following it.
626630 block .markFree ()
627631 freeCurrentObject = true
628632 gcFrees ++
629- freeBytes += bytesPerBlock
633+ freed ++
630634 case blockStateTail :
631635 if freeCurrentObject {
632636 // This is a tail object following an unmarked head.
633637 // Free it now.
634638 block .markFree ()
635- freeBytes += bytesPerBlock
639+ freed ++
636640 }
637641 case blockStateMark :
638642 // This is a marked object. The next tail blocks must not be freed,
@@ -644,6 +648,8 @@ func sweep() (freeBytes uintptr) {
644648 freeBytes += bytesPerBlock
645649 }
646650 }
651+ gcFreedBlocks += freed
652+ freeBytes += uintptr (freed ) * bytesPerBlock
647653 return
648654}
649655
@@ -690,6 +696,8 @@ func ReadMemStats(m *MemStats) {
690696 m .Mallocs = gcMallocs
691697 m .Frees = gcFrees
692698 m .Sys = uint64 (heapEnd - heapStart )
699+ m .HeapAlloc = (gcTotalBlocks - gcFreedBlocks ) * uint64 (bytesPerBlock )
700+ m .Alloc = m .HeapAlloc
693701}
694702
695703func SetFinalizer (obj interface {}, finalizer interface {}) {
0 commit comments