Explain escape analysis. Which of these allocate on the heap?
Question 389HardGo 1.22 to 1.25
type Point struct{ X, Y int }
func a() Point { p := Point{1, 2}; return p } // (1)
func b() *Point { p := Point{1, 2}; return &p } // (2)
func c() int { p := &Point{1, 2}; return p.X } // (3)
func d(n int) []int { return make([]int, n) } // (4)
func e() []int { return make([]int, 8) } // (5) returned
func f() { s := make([]int, 8); _ = s[0] } // (6)
func g(p Point) { fmt.Println(p) } // (7)
Answers (confirm with go build -gcflags=-m):
- Stack — returned by value (copied).
- Heap — "moved to heap: p"; the pointer outlives the frame.
- Stack — the pointer never leaves
c;&alone doesn't force heap allocation. - Heap — the slice escapes via return. (Non-escaping
makewith a non-constant size also went to the heap before Go 1.25; since 1.25 the compiler can use a small 32-byte stack buffer when the requested size fits, falling back to the heap otherwise.) - Heap — escapes via return, even though the size is constant.
- Stack — constant, small (< 64 KiB), doesn't escape.
- Heap —
pis converted toanyfor a variadic...anyparameter, and fmt's arguments escape.
Other common escape causes: storing a pointer in a global, a heap object, or a channel; closures capturing variables that outlive the function; calling methods via interfaces (the compiler can't see the callee); slices whose backing array grows via append beyond a stack-known size. Why it matters: each heap allocation costs malloc time plus future GC work. Interviewer is looking for: "the compiler decides, not new vs &", and the habit of verifying with -m and allocs/op instead of guessing.
More on Performance, Profiling & Testing
- Q387What is Profile-Guided Optimization (PGO) in Go, and how do you use it?
- Q388How does inlining work in the Go compiler, how do you inspect it, and what prevents a function from being inlined?
- Q390What is bounds check elimination (BCE), how do you see it, and how can you help the compiler?
- Q391What are the tradeoffs of sync.Pool? When does it help, and when does it hurt?
- Q392Compare the performance of string concatenation approaches: +, fmt.Sprintf, strings.Builder, bytes.Buffer, strings.Join.
- Q393What does this program do?