The Go garbage collector is a concurrent, non-blocking, tri-color mark-and-sweep collector that runs in parallel with your program to automatically reclaim unused memory. It operates in three phases: a concurrent mark phase where it identifies live objects, a brief stop-the-world sweep phase to free dead objects, and a concurrent write barrier that tracks changes during the mark phase to ensure safety. You can tune its behavior using the GOGC environment variable to control the percentage of heap growth that triggers a collection, or use runtime.GC() to force an immediate cycle.
How the Go Garbage Collector Works Internally
Go uses a concurrent, non-blocking tri-color mark-and-sweep garbage collector to automatically manage memory and reclaim unused objects without stopping the application.