Go: How Does the Garbage Collector Mark the Memory?

Vincent
A Journey With Go
Published in
5 min readNov 3, 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.”

The Go garbage collector is responsible for collecting the memory that is not in use anymore. The implemented algorithm is a concurrent tri-color mark and sweep collector. In this article, we will see in detail the marking phase, along with the usage of the different colors.

You can find more information about the different types of garbage collector in “Visualizing Garbage Collection Algorithms” by Ken Fox.

Marking phase

This phase performs a scan of the memory to know which blocks are still in use by our code and which ones should be collected.

However, since the garbage collector can run concurrently with our Go program, it needs a way to detect potential changes in the memory while scanning. To tackle that potential issue, an algorithm of write barrier is implemented and will allow Go to track any pointer changes. The only condition to enable write barriers is to stop the program for a short time, also called “Stop the World”:

--

--

No responses yet