Go: How Does Go Stop the World?

Vincent
A Journey With Go
Published in
4 min readJan 15, 2020

--

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.

A “Stop the World” (STW) is a crucial phase in some garbage collector algorithms to get track of the memory. It suspends the execution of the program to scan the memory roots and add write barriers. Let’s review how it works internally and the potential issues it could face.

Stop the World

Stopping the program means stopping the running goroutines. Here is a simple program that will “Stop the World”:

func main() {
runtime.GC()
}

Running the garbage collector will trigger two “Stop the World” phases.

For more information about the garbage collector cycle, I suggest you read my article “Go: How Does the Garbage Collector Mark the Memory?

The first step of this phase is to preempt all running goroutines:

goroutines preemption

Once the goroutines are preempted, they will be stopped at a safe point. Meanwhile, the processors P — running code or in the idle list — will be marked as stopped to not be used to run any code:

--

--