What is the Go garbage collector and how does it manage memory?
Learn how Go's concurrent tri-color mark-and-sweep garbage collector reclaims memory, plus GOGC and GOMEMLIMIT tuning explained with clear examples.
Expected Interview Answer
The Go garbage collector is a concurrent, tri-color mark-and-sweep collector that automatically reclaims heap memory no longer reachable by the program, so developers never manually free objects.
It runs mostly concurrently with your application, marking live objects reachable from roots (stacks, globals) and sweeping the rest for reuse. Go prioritizes low latency over throughput, using a write barrier to track pointer changes during marking and pacing collection so heap growth stays near the GOGC target (default 100%, meaning collect when the heap doubles). Since Go 1.19 a soft memory limit (GOMEMLIMIT) can also trigger collection to cap total memory.
- Eliminates manual memory management and whole classes of use-after-free bugs
- Concurrent design keeps stop-the-world pauses typically under a millisecond
- Automatic pacing balances CPU cost against heap growth
- GOGC and GOMEMLIMIT give tunable control over the memory/CPU trade-off
- No memory leaks from forgotten frees, only from unintended reachability
AI Mentor Explanation
Think of a busy dressing room during a long innings: as batters get out, their used gloves, pads, and towels pile up on the benches. Rather than each player tidying up, a dedicated attendant continuously walks through, notes which kit still belongs to batters yet to come in, and clears everything else away for reuse — all while the match keeps going, never halting play.
Step-by-Step Explanation
Step 1
Allocation
Objects that escape to the heap are allocated by the runtime, and the collector tracks total heap growth against the GOGC pacing target.
Step 2
Mark setup
When the heap hits the target, the GC briefly stops the world to enable the write barrier and scan stack roots, then resumes.
Step 3
Concurrent marking
Worker goroutines traverse reachable objects using tri-color marking (white, grey, black) while the program keeps running.
Step 4
Write barrier
Any pointer written during marking is recorded so newly reachable objects are not missed, preserving correctness.
Step 5
Sweeping
Unmarked (white) objects are reclaimed lazily as memory is needed, and their spans are made available for future allocations.
What Interviewer Expects
- Knows Go uses a concurrent tri-color mark-and-sweep collector
- Can explain the role of the write barrier during concurrent marking
- Understands GOGC pacing and the GOMEMLIMIT soft limit
- Aware that pauses are short and collection is mostly concurrent
- Distinguishes stack allocation from heap-managed memory
Common Mistakes
- Claiming Go uses reference counting instead of mark-and-sweep
- Saying the GC stops the world for the entire collection
- Confusing GOGC (a ratio) with an absolute memory cap
- Believing you must manually free memory in Go
- Assuming every allocation goes on the heap and is collected
Best Answer (HR Friendly)
“Go's garbage collector automatically cleans up memory the program no longer uses, so developers don't free it by hand. It works alongside the running program with very short pauses, and you can tune how aggressively it runs.”
Code Example
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
func main() {
// Collect when the heap grows 50% since the last GC (default is 100).
debug.SetGCPercent(50)
// Cap total memory the runtime targets (Go 1.19+).
debug.SetMemoryLimit(256 << 20) // 256 MiB
_ = make([]byte, 10<<20) // allocate 10 MiB on the heap
runtime.GC() // force a collection cycle
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("HeapAlloc=%d KiB, NumGC=%d\n", m.HeapAlloc/1024, m.NumGC)
}Follow-up Questions
- How does the tri-color marking algorithm avoid collecting live objects?
- What does the GOGC environment variable control and what is its default?
- When would you set GOMEMLIMIT and what are the risks?
- What is a write barrier and why is it needed during concurrent marking?
- How can you diagnose GC pressure in a production Go service?
MCQ Practice
1. Which algorithm does the Go garbage collector primarily use?
Go uses a concurrent, non-generational tri-color mark-and-sweep collector that runs mostly alongside the program.
2. What does the default GOGC value of 100 mean?
GOGC=100 triggers a collection when live heap growth reaches 100% (a doubling) relative to the previous cycle's live size.
3. Why does Go use a write barrier during garbage collection?
The write barrier records pointer updates during concurrent marking so objects made reachable mid-cycle are still marked live.
Flash Cards
What kind of GC does Go use? — A concurrent, non-generational tri-color mark-and-sweep collector.
What does GOGC control? — The heap growth ratio that triggers the next collection; default 100 means collect when the heap doubles.
What is GOMEMLIMIT? — A soft memory limit (Go 1.19+) that makes the GC run more aggressively to keep total memory under the target.
Why a write barrier? — It records pointer writes during concurrent marking so newly reachable objects are not swept by mistake.
Continue Learning
Related Interview Questions
What is escape analysis in Go and how does it decide stack vs heap allocation?
hard
What are goroutines and how do they differ from OS threads?
medium
What is the difference between concurrency and parallelism in Go?
medium
What are channels in Go and how do they enable communication between goroutines?
medium