What is escape analysis in Go and how does it decide stack vs heap allocation?
Understand Go escape analysis: how the compiler decides stack vs heap allocation, what causes values to escape, and how to inspect it with -gcflags='-m'.
Expected Interview Answer
Escape analysis is a compile-time technique the Go compiler uses to decide whether a value can live on the goroutine's stack or must be allocated on the heap, based on whether its lifetime escapes the function that created it.
If the compiler can prove a value does not outlive its function and no reference to it leaks out, it places the value on the stack, which is cheap to allocate and freed automatically when the function returns. If a pointer to the value escapes — for example by being returned, stored in a global, sent to a channel, or captured by a closure that outlives the frame — the value escapes to the heap and becomes the garbage collector's responsibility. You can inspect these decisions with go build -gcflags='-m'.
- Stack allocation is far cheaper than heap allocation and needs no GC
- Reduces garbage collector pressure and pause frequency
- Happens automatically at compile time with no code changes
- Inspectable via -gcflags='-m' to guide optimization
- Enables writing idiomatic code without manual memory placement
AI Mentor Explanation
Think of gear a batter uses only during their own innings versus gear that must be handed on. A helmet worn and returned at the crease can stay in the dugout locker and be cleared the moment the batter is out — like a stack value. But a signed bat promised to a fan leaves the ground entirely, so it must be logged in a permanent store, just as a value whose reference escapes must move to the heap.
Step-by-Step Explanation
Step 1
Analyze usage
During compilation, the compiler traces how each value and pointer flows through the function.
Step 2
Check for escapes
It looks for references that leave the frame: returns of pointers, storage in globals, channel sends, or closure captures.
Step 3
Prove locality
If no reference outlives the function, the value is safe to place on the stack.
Step 4
Promote to heap
If any reference escapes, the value is allocated on the heap so it remains valid after the function returns.
Step 5
Inspect decisions
Run go build -gcflags='-m' to see which values escape and why, then refactor hot paths if needed.
What Interviewer Expects
- Defines escape analysis as a compile-time decision, not runtime
- Lists concrete ways a value escapes (return pointer, global, channel, closure)
- Knows stack allocation avoids GC while heap allocation does not
- Can use -gcflags='-m' to inspect escape decisions
- Understands the performance motivation for keeping values on the stack
Common Mistakes
- Thinking escape analysis happens at runtime rather than compile time
- Assuming returning a pointer always causes a heap allocation regardless of context
- Believing you control stack vs heap directly with a keyword
- Confusing escape analysis with garbage collection itself
- Ignoring that closures capturing variables can force escapes
Best Answer (HR Friendly)
“Escape analysis is how Go's compiler decides where a value should live in memory. If the value is only used inside a function it stays on the fast stack; if it needs to survive longer, it moves to the heap where the garbage collector manages it.”
Code Example
package main
import "fmt"
// x does NOT escape: used and discarded within the function -> stack.
func sumLocal() int {
x := 42
return x
}
// p escapes: a pointer is returned, so the value must outlive the call -> heap.
func newCounter() *int {
p := new(int)
*p = 1
return p
}
func main() {
fmt.Println(sumLocal())
fmt.Println(*newCounter())
// Inspect: go build -gcflags='-m' main.go
// Output notes: "p escapes to heap"
}Follow-up Questions
- How do you use go build -gcflags='-m' to inspect escapes?
- Does returning a pointer always cause a heap allocation? Why or why not?
- How can closures cause variables to escape to the heap?
- Why is stack allocation cheaper than heap allocation in Go?
- How does escape analysis interact with the garbage collector's workload?
MCQ Practice
1. When does escape analysis run?
Escape analysis is a static, compile-time analysis performed by the Go compiler to decide allocation placement.
2. Which of these typically causes a value to escape to the heap?
Returning a pointer means the value must outlive the function, so it is generally allocated on the heap.
3. Which flag reveals the compiler's escape decisions?
Building with -gcflags='-m' prints optimization notes including which values escape to the heap and why.
Flash Cards
What is escape analysis? — A compile-time analysis deciding whether a value can live on the stack or must go on the heap.
What makes a value escape? — A reference that outlives the function: returned pointers, globals, channel sends, or closure captures.
How do you inspect escapes? — Build with go build -gcflags='-m' to see per-value escape decisions and reasons.
Why prefer stack allocation? — Stack allocation is cheap, freed automatically on return, and creates no garbage collector work.