tezvyn:

Go's Garbage Collector: The Concurrent Cleaner

AI-drafted, machine-checkedSource: Wikipedia: Garbage collection (computer science)intermediate
Go's Garbage Collector: The Concurrent Cleaner

Go's garbage collector is a concurrent cleaning crew, freeing memory while your program runs. It automatically reclaims unused memory, preventing leaks without manual `free()` calls. The footgun is assuming it's free; excessive allocations create GC pressure.

WHY IT EXISTS: Go was designed for building concurrent network services. Manual memory management, like in C, is a major source of bugs and security vulnerabilities. Traditional garbage collectors often required long "stop-the-world" pauses, which is unacceptable for a server that needs to stay responsive. Go's GC was created to solve this by providing safe, automatic memory management with very short, predictable pause times.

THE MENTAL MODEL: Think of Go's GC as a concurrent tri-color painting process. Initially, all memory objects are considered "white" (potential garbage). The GC starts at the roots of your program—global variables and active goroutine stacks—and traces every object that is reachable. As it traverses, it paints reachable objects "gray" (to be scanned) and then "black" (scanned and confirmed live). This marking happens while your program is still running.

HOW IT WORKS: Go uses a concurrent mark-and-sweep algorithm. The cycle starts with a brief stop-the-world pause, often microseconds, to enable a write barrier and set up marking. The write barrier is a mechanism that notifies the GC if your program modifies pointers while marking is in progress, preventing it from incorrectly collecting a live object. The marking phase then runs concurrently with your goroutines. After marking is complete, another short pause finalizes the process, and the sweeping phase—reclaiming the memory of "white" objects—also happens concurrently.

WHEN TO USE IT: The garbage collector is a core feature of the Go runtime; it's always active. You benefit from it in any Go program, especially in long-running services like APIs and web servers where it prevents memory leaks without manual intervention. Its low-latency design is optimized for systems that prioritize responsiveness.

WHEN NOT TO USE IT: You cannot disable the GC in Go. If your application has hard real-time constraints where any non-deterministic pause, even for microseconds, is unacceptable (e.g., high-frequency trading, aerospace control systems), Go might not be the right tool. In such cases, languages with explicit memory management like Rust or C++ are often preferred.

ONE CANONICAL EXAMPLE: A web server handles an incoming HTTP request. It allocates memory for a request context, headers, and a JSON payload. Once the server sends the response and the handler function returns, none of this memory is accessible anymore. On the next GC cycle, the collector will see these objects are unreachable and will reclaim their memory. A common performance issue is creating excessive garbage in this loop, forcing the GC to run too frequently.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.