Go Reference ยท Concepts
Escape analysis in Go
How the Go compiler decides whether a value lives on the stack or the heap, what makes variables escape, how to see decisions with -gcflags=-m and measure allocations.
Go has no stack or heap keyword. When you write x := T{} or new(T) or &T{}, the compiler decides where the value lives. It runs escape analysis: if it can prove that a value is never used after its function returns, the value can live in the function's stack frame, which is cheap and freed automatically. If the value might outlive the call, it "escapes" and is allocated on the heap, where the garbage collector manages it.
Escape analysis is an optimization, not a language rule. Your program behaves the same either way; only speed and garbage-collector load differ. Taking the address of a variable is safe even when the variable is local, because the compiler moves it to the heap when needed.
$ go build -gcflags=-m main.go ./main.go:9:6: moved to heap: n
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
What makes a value escape
- Returning the address of a local variable, or a slice or map backed by local storage.
- Storing a pointer in a value that itself escapes: a global, a heap object, a channel send, a field of an escaping struct.
- Converting a non-pointer value to an interface when the interface escapes (
fmt.Println(x)often allocates for this reason). - Capturing a variable in a closure that outlives the function (for example, passed to
goor stored). - Dynamic size:
make([]T, n)with a non-constantnor one above the compiler's size limit (64 KiB for implicit variables, 10 MiB for explicitnew/&T{}). - Calling through an interface or function value, where the compiler cannot see what the callee does with the argument.
Usage notes
- See decisions with
go build -gcflags=-m(or-m -mfor the reasons). Messages:escapes to heap,moved to heap: x,does not escape. - Measure with
testing.AllocsPerRun, a benchmark withb.ReportAllocs(), orgo test -benchmem. Do not guess. - Inlining matters: a small function inlined into its caller often lets values stay on the stack.
//go:noinlineis used here only so the experiment is not optimized away. - Pointers are not automatically slower or faster. A value copy of a small struct is often cheaper than a pointer plus an allocation. Choose by semantics first.
- Stacks in Go are growable. Large frames are copied when the stack grows, which is invisible to your code but is one reason huge locals go to the heap.
- Reducing allocation is useful in hot paths only. Profile first (
pprofallocation profile), then change code. - Escape analysis is conservative: if it cannot prove safety it allocates. Results can change between Go versions.