🐹 Go (Golang)
Advanced
How does Go's garbage collector work?
Answer
Go uses a concurrent tri-color mark-and-sweep garbage collector. It divides objects into three sets: white (not yet seen), grey (reachable but children not yet scanned), and black (reachable with all children scanned). The GC starts with roots (globals, stack variables), marks them grey, then iteratively moves grey objects to black while colouring their children grey, until no grey objects remain. White objects at the end are unreachable and swept. The mark phase runs concurrently with the program (using write barriers to maintain correctness), minimizing stop-the-world pauses to sub-millisecond levels — critical for latency-sensitive services. Tune GC pressure with the GOGC environment variable.