Go uses a concurrent, non-generational, non-compacting, tri-color mark-and-sweep garbage collector. It runs alongside your goroutines, keeps stop-the-world pauses very short, and frees heap memory that is no longer reachable from any root. You tune it with GOGC (how much the heap may grow between cycles) and GOMEMLIMIT (a soft total memory cap), not with a pile of collector flags.
The collector is automatic, so most programs never touch it. It still pays to understand what triggers a cycle, what it costs, and how to measure it before you change any setting.
Go allocates objects either on a goroutine stack or on the heap. The compiler's escape analysis decides which: a value that does not outlive its function usually stays on the stack and costs the collector nothing. Values that escape (returned pointers, values stored in interfaces, captured by long-lived closures) go to the heap, and only heap objects are managed by the collector.
Two design choices shape its behaviour:
During marking, every object is conceptually white, grey or black.
| Color | Meaning |
|---|---|
| White | Not yet reached. Still white at the end means garbage. |
| Grey | Reached, but the objects it points to have not been scanned yet. |
| Black | Reached and fully scanned. |
A cycle runs in these phases:
Because your code keeps running during the mark phase, it could hide a live object from the collector by moving a pointer. Go prevents this with a hybrid write barrier (since Go 1.8), which combines a deletion barrier and an insertion barrier. This also removed the need to rescan goroutine stacks, which is why pauses are now typically a fraction of a millisecond.
If a goroutine allocates faster than the collector can mark, the runtime makes it do part of the marking work itself. These mutator assists slow the allocating goroutine and are the main way a GC cycle shows up as latency in your code.
The collector starts a cycle when the heap reaches a target computed from the live heap after the previous cycle:
With the default GOGC=100, the heap may roughly double before the next cycle. Lowering it trades CPU for memory; raising it does the opposite. GOGC=off disables the proportional trigger.
Since Go 1.19 you can also set a soft memory limit with GOMEMLIMIT or debug.SetMemoryLimit. The runtime collects more aggressively as total Go memory approaches the limit. It is a soft limit, so it will not stop an allocation that is genuinely needed, and the collector's CPU use is capped to avoid a death spiral.
A common pattern for containers is GOGC=off with a GOMEMLIMIT slightly below the container limit, but only for programs whose live heap is well under that limit. Otherwise the collector will run back to back near the limit.
Never tune blind. Three tools cover most needs.
Set GODEBUG=gctrace=1 to print one line per cycle to stderr, with pause times, heap sizes and CPU split:
Read counters in-process with runtime.ReadMemStats, or with the lower-overhead runtime/metrics package:
runtime.ReadMemStats briefly stops the world, so avoid calling it in hot paths. For production, expose runtime/metrics through your metrics system, and use go tool pprof heap and allocation profiles (net/http/pprof) to find who allocates.
The cheapest garbage is the garbage you never create.
make([]T, 0, n).bytes.Buffer.Reset or sync.Pool for short-lived, frequently allocated objects. A pool can be emptied at any GC, so never rely on it to hold state.strings.Builder instead of repeated string concatenation.go build -gcflags=-m.Go 1.25 added an experimental collector called Green Tea, enabled with GOEXPERIMENT=greenteagc, which scans small objects more cache-efficiently. Check the release notes for your Go version to see whether it is on by default, since the design continues to evolve.
Yes, but only two short ones per cycle (sweep termination and mark termination). They are usually well under a millisecond because the heavy work happens concurrently.
runtime.GC() runs a full cycle and blocks until it finishes. It is useful in tests and benchmarks, but in production you should let the pacer decide.
No. Objects never move, which keeps pointers stable and avoids compaction pauses. The allocator uses size classes to limit fragmentation.
Use GOGC to trade memory for CPU in general, and add GOMEMLIMIT as a safety net against running out of memory. They work together.
Go's collector is a concurrent, tri-color, non-moving mark-and-sweep design with a hybrid write barrier and very short pauses. Its cost scales with live heap size, so the best optimisation is to allocate less. Measure with gctrace, runtime/metrics and pprof first, then adjust GOGC and GOMEMLIMIT if the numbers justify it.