Garbage Collector (Tri-color)
Concurrent tri-color mark-and-sweep GC providing sub-millisecond stop-the-world pauses.
“A team of janitors cleaning an office building while workers are working: they tag active desks with color badges (White, Grey, Black) and recycle unreferenced desks in the background.”
Key Concepts & Rules To Remember
- Runs concurrently with your application goroutines.
- STW (Stop-The-World) pauses are typically under 1 millisecond.
- Tuned via `GOGC` environment variable (default 100).
1. Understanding Garbage Collector (Tri-color)
Concurrent tri-color mark-and-sweep GC providing sub-millisecond stop-the-world pauses. In Go, garbage collector (tri-color) is designed around clarity and high runtime efficiency.
// GOGC=100 triggers GC when heap doubles
// GOMEMLIMIT sets hard memory ceiling in modern GoHow Cloudflare Reduced Go GC Latency by 90% Using sync.Pool
Allocating millions of temporary 32 KB byte buffers per second triggered frequent Garbage Collector Stop-The-World (STW) mark phases, causing 50ms latency spikes.
Replaced ephemeral heap buffer allocations with a global `sync.Pool`. Buffers are checked out upon request arrival and returned to the pool immediately upon completion.
var bufferPool = sync.Pool{
New: func() any {
b := make([]byte, 32*1024)
return &b
},
}
func handleRequest(r io.Reader) {
bufPtr := bufferPool.Get().(*[]byte)
defer bufferPool.Put(bufPtr) // Return to pool
// Process request with *bufPtr...
}Common Beginner Pitfalls & Mistakes
Knowledge Check
Verify your understanding with these interactive practice questions.
Quizzes
Quick checks for understanding
Multiple-choice with inline explanations—expand to see why.
Finished this lesson?
Mark it as complete to track your overall Go mastery.