Read Go Escape Analysis (-gcflags=-m) and Tune GOMEMLIMIT
Inspect heap escapes with go build -gcflags=-m and set GOGC and GOMEMLIMIT to prevent container OOM kills. After this, memory usage and GC behavior are predictable in production.
Imagine your Go service runs inside a 1 GiB Kubernetes pod and normally uses 180 MiB of live memory. During a 30-second traffic burst, allocations spike—and even though most of those objects become unreachable garbage milliseconds later, Kubernetes suddenly kills the pod with OOMKilled (Exit Code 137).
Why did that happen? Because by default Go's garbage collector runs when the heap doubles (GOGC=100), completely unaware of your container's 1 GiB memory ceiling. Understanding escape analysis (why objects land on the heap in the first place) and tuning GOMEMLIMIT prevents both unnecessary GC work and container OOM kills.
Skip ahead
Jump to reading -gcflags="-m" or setting GOMEMLIMIT safely in containers.
1. Stack vs. heap: what go build -gcflags="-m" tells you
Variables that stay on a function's stack are freed for free when the function returns. Variables that escape to the heap must be allocated, tracked, and swept by the garbage collector. Watch how returning a pointer forces Go's compiler to move a variable to the heap:
package main
func staysOnStack() int {
x := 42
return x + 1
}
func escapesToHeap() *int {
y := 99
return &y
}
func main() {
_ = staysOnStack()
_ = escapesToHeap()
}
$ go build -gcflags="-m" .
# example.com/escape
./main.go:3:6: can inline staysOnStack
./main.go:8:6: can inline escapesToHeap
./main.go:13:6: can inline main
./main.go:14:18: inlining call to staysOnStack
./main.go:15:19: inlining call to escapesToHeap
./main.go:9:2: moved to heap: y
Look at line 9: moved to heap: y. Because escapesToHeap returns &y, callers can keep reading y long after escapesToHeap's stack frame disappears—so the compiler allocates y on the heap. Meanwhile, x stays on the stack and costs zero GC work.
2. How GOGC and GOMEMLIMIT work together
| Environment variable | Default | How it behaves in production |
|---|---|---|
GOGC |
100 |
Triggers GC when live heap grows by 100% since the last cycle. Raising to 200 trades RAM for less GC CPU. |
GOMEMLIMIT |
Unset | Soft total-memory target (e.g. 850MiB on a 1 GiB pod). As memory nears the limit, Go runs GC more often to avoid an OOM kill. |
Why set GOMEMLIMIT=850MiB instead of 1024MiB on a 1 GiB pod? Because the Go runtime also uses non-heap memory (goroutine stacks, OS thread stacks, cgo buffers, memory-mapped files) that counts toward Kubernetes's cgroup RSS limit. Leaving 10–20% headroom gives the GC room to collect before the kernel kills your process.
💡 Tip
With GOMEMLIMIT protecting your ceiling, you can often raise GOGC=200 on memory-rich services so normal steady-state traffic runs half as many garbage collections without risking OOMs during spikes.
Common mistakes
- Boxing values into
anyon hot loops: Passing small structs intofmt.Sprintfor interface loggers often forces stack values to escape to the heap. - Setting
GOMEMLIMITtoo close to your live heap size: If your service genuinely needs 750 MiB of live data and you setGOMEMLIMIT=760MiB, Go will thrash CPU running back-to-back GCs trying to stay under budget. - Micro-optimizing cold-path
moved to heapmessages: Usepprof -alloc_spaceor-benchmemfirst; only eliminate escapes inside hot loops.
⚠ Warning
Never set GOGC=off without a carefully load-tested GOMEMLIMIT—and monitor go_gc_cycles_total so you catch GC thrashing early if live memory grows.
What happens if you skip this
Hot-path escapes burn unnecessary GC CPU, and containers running without GOMEMLIMIT get OOM-killed during brief traffic spikes even when most of the allocated heap is already collectable garbage.
Related course: Go Advanced.
About
Inspect heap escapes with go build -gcflags=-m and set GOGC and GOMEMLIMIT to prevent container OOM kills. After this, memory usage and GC behavior are predictable in production.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.