Go manages memory automatically. The compiler decides whether each value lives on a goroutine stack or on the heap using escape analysis, and a concurrent, tri-color mark-and-sweep garbage collector frees heap memory that is no longer reachable. You tune it mainly with GOGC and GOMEMLIMIT, and you improve performance by allocating less, not by calling the collector by hand.
Go does not use reference counting, a common misconception. The collector traces reachability from roots such as stacks and globals. This article explains the model and the practical techniques to measure and reduce memory usage.
Each goroutine has a small stack that grows and shrinks as needed. Values that do not outlive the function call can stay on the stack, which is cheap and needs no garbage collection. Values whose address escapes, for example by being returned as a pointer or stored in an interface that outlives the call, are allocated on the heap.
Ask the compiler to explain its decisions with go build -gcflags=-m:
Passing a value to fmt.Println also makes it escape because it is converted to an interface. Escaping is not a bug, but each heap allocation adds work for the collector.
Go's collector is concurrent, non-generational and non-moving. A cycle has these phases:
Because it does not move objects, pointers stay valid and there is no compaction. Pauses are typically very short, and the main cost is CPU used by the marker plus allocation slowdowns when the collector needs help from busy goroutines.
GOGC sets how much the heap may grow before the next cycle. The default is 100, meaning the collector starts when the heap is about double the live data after the previous collection. A higher value uses more memory and less CPU; a lower value does the reverse.
GOMEMLIMIT (Go 1.19 and newer) sets a soft limit on total Go memory. The collector works harder as the limit approaches. This is valuable in containers.
You can also set them in code with debug.SetGCPercent and debug.SetMemoryLimit. Set the memory limit a little below the container limit, leaving room for non-Go memory such as cgo allocations and stacks.
| Setting | Effect | Use when |
|---|---|---|
Higher GOGC | Fewer cycles, more RAM | CPU bound services with spare memory |
Lower GOGC | More cycles, less RAM | Memory constrained hosts |
GOMEMLIMIT | Soft cap on memory | Containers with a hard limit |
GOGC=off plus limit | Collect only when needed | Batch jobs with predictable data |
Measure before you optimize. These tools ship with Go:
GODEBUG=gctrace=1 prints one line per GC cycle with heap sizes and pause times.runtime.ReadMemStats and the runtime/metrics package expose heap size, allocation counts and GC statistics.go test -bench . -benchmem reports allocations per operation.net/http/pprof or runtime/pprof heap profiles show who allocates memory (go tool pprof).make([]T, 0, n).bytes.Buffer, strings.Builder and sync.Pool for short-lived, frequently allocated objects.A sync.Pool may drop its contents at any collection, so use it only as a cache of reusable scratch objects, never for state you must keep. Always reset objects before returning them.
Go programs still leak when something keeps a reference alive. Typical causes are goroutines blocked forever on a channel, global maps or caches that never evict, time.Ticker values never stopped, and slices that pin large arrays. Use goroutine and heap profiles to find them, and give goroutines a way to exit with context.
Calling runtime.GC() manually is rarely useful outside tests and benchmarks. Finalizers are also fragile; prefer runtime.AddCleanup (Go 1.24) or explicit Close methods for releasing resources.
No. Go uses a tracing, concurrent mark-and-sweep collector. Objects are freed when they are unreachable from roots, not when a count reaches zero.
The next collection starts when the heap has grown by 100 percent over the live heap measured after the last cycle.
It sets a soft memory ceiling for the Go runtime, so the collector becomes more aggressive near the limit. It is useful in containers to avoid out-of-memory kills.
Almost never. The runtime schedules collections itself. Reduce allocations or adjust GOGC and GOMEMLIMIT instead.
Go allocates on the stack when it can, on the heap when it must, and reclaims heap memory with a concurrent mark-and-sweep collector. Measure with gctrace, -benchmem and pprof, tune with GOGC and GOMEMLIMIT, and reduce pressure by preallocating, reusing buffers and avoiding needless pointers and interface conversions.