Go heap vs stack — escape analysis with slices

Stack versus heap allocation in Go stayed fuzzy until I read the articles in the references at the end. Here is a condensed version.

What the Go FAQ says

How do I know whether a variable is allocated on the heap or the stack?

From a correctness standpoint, you do not need to know. Each variable in Go exists as long as there are references to it. The storage location chosen by the implementation is irrelevant to the semantics of the language.

The storage location does affect efficient code. When possible, the Go compilers allocate variables that are local to a function in that function’s stack frame. If the compiler cannot prove that the variable is not referenced after the function returns, it must allocate on the garbage-collected heap to avoid dangling pointer errors. If a local variable is very large, the heap may be a better choice than the stack.

In current compilers, taking the address of a variable makes heap allocation likely. Escape analysis can still keep some of those variables on the stack when they clearly do not outlive the function.

How do I know whether a variable is allocated on the heap or the stack?

From a correctness standpoint, you don’t need to know. Each variable in Go exists as long as there are references to it. The storage location chosen by the implementation is irrelevant to the semantics of the language.

The storage location does have an effect on writing efficient programs. When possible, the Go compilers will allocate variables that are local to a function in that function’s stack frame. However, if the compiler cannot prove that the variable is not referenced after the function returns, then the compiler must allocate the variable on the garbage-collected heap to avoid dangling pointer errors. Also, if a local variable is very large, it might make more sense to store it on the heap rather than the stack.

In the current compilers, if a variable has its address taken, that variable is a candidate for allocation on the heap. However, a basic escape analysis recognizes some cases when such variables will not live past the return from the function and can reside on the stack.

Example adapted from Go slice pitfalls when passing as parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
"fmt"
"unsafe"
)

func main() {
a := []int{}
a = append(a, 7, 8, 9)
fmt.Printf("len: %d cap:%d data:%+v \n", len(a), cap(a), a)
println(&a, 1)
ap(a)
//apRef(&a)
fmt.Printf("len: %d cap:%d data:%+v \n", len(a), cap(a), a)
println(&a, 2)
p := unsafe.Pointer(&a[2])
q := uintptr(p) + 4
t := (*int)(unsafe.Pointer(q))
fmt.Println(*t)
}

func ap(a []int) {
a = append(a, 10)
println(&a, 3)
}

func apRef(a *[]int) {
*a = append(*a, 10)
println(a, 3, cap(*a))
}

Run on the playground

Quick read of ap vs apRef:

  • Go passes function arguments by value. In ap, the slice header is copied.
  • The compiler prefers stack frames for locals when it can — fast and reused on the next call.
  • ap mutates its copy in its own frame. After ap returns, that frame storage is undefined; main still sees the original header unless growth shared the backing array.
  • apRef takes *[]int. The compiler cannot assume the pointer dies at return, so the pointed-to data often lives on the heap. Updates visible in main stay valid while references remain.

So ap‘s append of 10 may not show up in main‘s length; apRef updates heap-backed state main still references.

The original article also discusses slice growth; worth a read.

Knowing how to check beats memorizing one example — practice the tools below.

Memory analysis:

  • Compiler escape log, e.g. go build -gcflags "-m -m" (go tool compile -help lists flags).
  • Benchmarks: go test -run none -bench AlgorithmOne -benchtime 3s -benchmem
  • Profiles: go test -run none -bench AlgorithmOne -benchtime 3s -benchmem -memprofile mem.out

References

Questions? Leave a comment below or email: newbvirgil@gmail.com