Lets understand how Golang handles variables and what this Escape Analysis is. Golang have 2 types of memory allocation datatypes used in go runtime environment called stack and heap.
The stack is faster for doing operations on(in and out). Variables inside a function that are accessed within it go to this stack. Eg:
package main
import "fmt"
func stackExample() int {
a := 10 // Variable `a` is allocated on the stack
b := 20 // Variable `b` is allocated on the stack
result := a + b // `result` is also allocated on the stack
return result // All stack variables are popped off when the function exits
}
func main() {
result := stackExample() // Function call happens, variables are pushed to the stack
fmt.Println(result) // Print the result
}
This is how the stack works in GoLang. Next is a heap. It's a dynamic memory ie, its size can be adjusted based on the requirements. Here is a simple example of heap allocation.
package main
import "fmt"
func heapExample() *int {
num := 42 // Variable `num` is created inside the function
return &num // Returning the address of `num`, which escapes the function
}
func main() {
ptr := heapExample() // The value pointed to by `ptr` is allocated on the heap
fmt.Println(*ptr) // Accessing the heap-allocated variable through the pointer
}
Here since the function returns a pointer to num
, the Go runtime detects that num
will be accessed outside of the function's scope. As a result, it allocates num
on the heap instead of the stack.
A Heap is used to store variables that are beyond the scope of a function or a goroutine. Sometimes when there value of a variable is very huge it's stored in a heap.
After understanding these two it's time to jump into escape analysis. There’s no way of knowing which variables will be allocated to the heap simply by reading the Go code. We can understand this with an 'Escape Analysis'. By escape, we mean whether the variable escapes the scope of a function determining whether it will be stored on a heap or stack.
Run the build command of the go code like this
go build -gcflags '-m -l'
-m - Flag to show escape analysis.
-l - Disable inlining to keep stack traces accurate.
Take this code:
package main
func escapeExample() *int {
x := 42
return &x // x escapes because its address is returned
}
func noEscapeExample() int {
y := 100
return y // y does not escape
}
func main() {
_ = escapeExample()
_ = noEscapeExample()
}
The results are something like these:
./main.go:4:10: &x escapes to heap
./main.go:12:13: main escapes to heap
So, why do we do all these? This analysis can prove to be useful when you are debugging for performance issues. Variables are better stored in a stack rather than in a heap. You can find where variables escape to the heap and refactor the code for better efficiency.
Hope you learned something new today.
I am currently building LiveAPI which is a tool for API document generation do try that out.
Thanks for reading.
Top comments (0)