Why does the Go GC have to pause?

discussion

Pardon my ignorance if this is an obvious question. I’ve been a systems programmer for years with C, Go, and Rust, and surprisingly I’ve been checking and second guessing myself about how much I REALLY know about how all of this stuff works under the hood.

The way I understand Go’s GC (simplified) is it will periodically freeze the program, walk over the memory blocks, check that there is nothing in the program that could still be referencing a given heap allocation, and then mark those blocks, freeing them when it can.

Why does this have to be synchronous? Or, maybe more accurately, why can’t this be done in parallel with the main program execution?

In the model in my head, something like this would work:

  1. Program is running, made a bunch of allocations, blah blah blah
  2. Runtime has a GC thread (an OS thread, not a green thread, so likely running on its own core)
  3. GC thread rapidly inspects the memory space of the app while it’s running (a lock on anything wouldn’t be necessary since it’s just inspecting the memory, if it changes under it while being inspected that run is just discarded)
  4. If it sees something is no longer referenced, it can destroy that memory block in a different thread while the app is running

Obviously assume here I’m talking about a multi-threaded OS and multi core CPU and not micro controllers where this is not possible.

Is there any reason that something like this is not possible or wouldn’t work?

Thanks in advance

Posted May 4, 2025, 4:35 PM UTC

12853
Loading comments…