Go: Memory Management and Memory Sweep

Vincent
A Journey With Go
Published in
4 min readNov 8, 2019

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ 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:

Zeroing the memory

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.

--

--