Garbage Collector Internals
Tri-color mark-sweep, GOGC, and GC pauses
Garbage Collector Internals is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Tri-color mark-and-sweep
Go's GC uses a concurrent tri-color mark-and-sweep algorithm. Objects are white (unvisited), grey (found but children unscanned), or black (fully scanned). White objects are garbage at sweep.
Concurrent GC
The Go GC runs mostly concurrently with application goroutines. This reduces stop-the-world (STW) pauses to sub-millisecond durations in most programs.
Write barrier
A write barrier intercepts pointer writes during the concurrent mark phase to maintain the invariant that black objects do not point to white objects. It adds a small overhead to every pointer write.
STW phases
Two short STW pauses per GC cycle: (1) marking setup (enable write barrier, snapshot goroutine stacks), (2) mark termination (ensure all greys are scanned). P99 is typically <1ms.
GOGC
The GC trigger is controlled by GOGC (default 100). It means "run GC when live heap size doubles from the previous GC." Setting GOGC=50 triggers more frequent GC with less memory; GOGC=200 less frequent GC with more memory.
GOMEMLIMIT
GOMEMLIMIT (Go 1.19+) sets a soft memory limit. When the heap approaches the limit, the GC runs more aggressively regardless of GOGC. Use it to prevent OOM kills in memory-constrained environments.
import "runtime/debug"
debug.SetMemoryLimit(512 * 1024 * 1024)Pacing
The GC paces itself to finish marking before the heap fills up relative to GOGC. If allocation rate is high, it increases the number of goroutines assisting in marking (mark assists).
runtime.GC()
Calling runtime.GC() forces an immediate GC cycle. Useful in tests to ensure finalizers run, or in benchmarks to start with a clean heap. Do not call it in production.
Finalizers
Register a function to run when an object is garbage-collected with runtime.SetFinalizer. Finalizers run in a dedicated goroutine after GC. Avoid them for resource cleanup — use Close() methods instead.
GC metrics
Read GC stats from runtime.ReadMemStats or expose them via Prometheus metrics. Key fields: NumGC, PauseNs, HeapAlloc, HeapInuse.
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("GC cycles: %d, last pause: %dns\n", m.NumGC, m.PauseNs[(m.NumGC+255)%256])Scavenger
A background scavenger returns idle heap memory to the OS. It runs concurrently and is paced to not impact foreground performance. The MADV_FREE or MADV_DONTNEED syscall is used.
Quick Check
What does GOMEMLIMIT do in Go 1.19+?
Recap: GC Internals
Key points:
- Concurrent tri-color mark-and-sweep; two short STW pauses
- GOGC controls heap growth ratio; default 100 (100% growth triggers GC)
- GOMEMLIMIT for container memory safety (Go 1.19+)
- runtime.ReadMemStats for GC metrics
Frequently asked questions
Is the “Garbage Collector Internals” lesson free?
Yes — the full text of “Garbage Collector Internals” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Garbage Collector Internals”?
Tri-color mark-sweep, GOGC, and GC pauses You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Garbage Collector Internals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.