Go: Memory Management and Memory Sweep
ℹ️ This article is based on Go 1.13. The notions about memory management discussed here are explained in my article “Go: Memory Management and Allocation.”
Sweeping the memory is a process that allows Go to know which memory segments are newly available for allocation. However, it does not clean the memory with resetting the bits to zero.
Zeroing the memory
The process of zeroing the memory — moving all bits to zero in the memory segment — is done on the fly during the allocation:
However, we could wonder what strategy Go uses to know which objects are available for allocation. Go actually tracks the free objects thanks to an internal bitmap in each span called allocBits
. Let’s review its workflow, starting with the initial state:
Performance-wise, allocBits
will represent the initial state and will remain the same, but it is helped by freeIndex
, an incremental counter that points to the first free slot.
Then, the first allocation starts:
freeIndex
is now incremented and knows the next free slot based on allocBits
.