Go's garbage collector (GC) automatically finds heap memory that the program can no longer reach and returns it for reuse, so you do not call free or track ownership by hand. It is a concurrent, tri-color, non-generational, non-moving mark-and-sweep collector designed for low pause times rather than maximum throughput.
Understanding what it does helps you write code that allocates less, tune it with GOGC and GOMEMLIMIT, and diagnose memory growth in production.
Go allocates values on the stack when the compiler can prove they do not outlive the function (escape analysis), and on the heap otherwise. Stack memory is released automatically when the function returns and does not involve the GC. The GC only manages the heap: it frees objects that are no longer reachable from goroutine stacks, globals or other live objects.
Each Buffer becomes unreachable after its loop iteration, so the next collection can reclaim it. You can see which values escape with go build -gcflags=-m.
A collection cycle has these phases:
During the mark phase the GC uses about 25% of the available CPU (GOMAXPROCS) on dedicated workers, and goroutines that allocate quickly can be drafted into doing assist work. The pauses themselves are usually well under a millisecond for typical services, while the work is spread across the cycle.
Two settings control the pacing.
| Setting | Meaning | Default |
|---|---|---|
GOGC | Heap growth target: a cycle starts when the heap reaches live heap plus GOGC percent | 100 |
GOMEMLIMIT | Soft limit on total Go memory, available since Go 1.19 | No limit |
With GOGC=100, if 100 MB is live after a cycle, the next one starts near 200 MB. A higher GOGC uses more memory and less CPU; a lower one does the opposite. GOMEMLIMIT makes the GC work harder as total memory approaches the limit, which suits containers. A common pattern is to set GOMEMLIMIT to roughly 90% of the container limit and leave GOGC at its default.
You can also set them in code with debug.SetGCPercent and debug.SetMemoryLimit.
Use the runtime package for counters, and GODEBUG=gctrace=1 for per-cycle logs.
runtime.GC() forces a collection, but you rarely need it outside tests and benchmarks. For production, use pprof heap profiles and the runtime/metrics package.
The cheapest garbage is the garbage you never create:
make([]T, 0, n) when you know the size.sync.Pool for short-lived, frequently allocated objects.nil in long-lived structures. A leaked goroutine keeps everything it references alive.go test -bench . -benchmem to see allocations per operation.Since Go 1.24 the weak package and runtime.AddCleanup offer safer tools than runtime.SetFinalizer for caches and resource cleanup, but you should still close files and connections explicitly with defer.
No. Go's collector is non-generational and non-moving, which keeps pointers stable and the implementation simple, at the cost of marking the whole live heap each cycle.
Only for two very short pauses per cycle. Most marking and sweeping happens concurrently with your goroutines, though allocation-heavy code can be slowed by GC assists.
Allocate less: preallocate, reuse buffers and avoid unnecessary pointers. Then adjust GOGC or GOMEMLIMIT based on profiles, not guesses.
Almost never. The runtime paces collections itself, and forced collections usually hurt latency. They are mainly useful in tests and memory benchmarks.
Go's garbage collector reclaims unreachable heap memory with a concurrent mark-and-sweep design that keeps pauses short. Stack values are free, heap values are scanned, and GOGC plus GOMEMLIMIT let you trade memory for CPU. Reduce allocations first, tune second, and measure with pprof and gctrace.