Variable escape in Golang
Golang compiler variable escape analysis
The Go language compiler will automatically decide whether to put a variable on the stack or the heap.
The compiler will do escape analysis. When it is found that the scope of the variable does not run out of the function scope,
it can be placed on the stack, and vice versa. allocated on heap
1 | package main |
When compiling, you can use the option -gcflags=-m to check the situation of variable escape.
1 | go build -gcflags=-m main.go |
We can see that new(Demo) escapes to heap indicates that local variables escape to the heap
Escape rules
Generally, when we assign a value to a reference class member in a reference class object, escape may occur.
It can be understood that accessing a reference object is actually indirectly accessed through a pointer,
but if you access the reference member inside again, there will be a second indirect access.
If you operate this part of the object, escape is very likely to occur.
Reference types in Go language include func (function type), interface (interface type),
slice (slice type), map (dictionary type), channel (pipeline type), * (pointer type), etc.
Escape Example
[]interface{} data type, assignment through [] will inevitably cause escape.
1
2
3
4
5
6package main
func main() {
data := []interface{}{100, 200}
data[0] = 100
}Let’s take a look at the escape results through compilation
1
2
3
4
5
6
7go build -gcflags=-m main.go
command-line-arguments
./main.go:3:6: can inline main
./main.go:4:23: []interface {}{...} does not escape
./main.go:4:24: 100 does not escape
./main.go:4:29: 200 does not escape
./main.go:5:2: 100 escapes to heapWe can see that data[0] = 100 escapes.
Escape example two
If the map[string]interface{} type attempts to assign a value, escape will definitely occur.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We can see that data[“key”] = 200 escaped.
- Escape example three
The map[interface{}]interface{} type attempts to assign values, which will cause the assignment of key and value to escape.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We can see that in data[100] = 200, both 100 and 200 escape.
- Escape example four
map[string][]string data type, assignment will cause []string to escape.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We can see that the []string{…} slice escaped.
- Escape example five
[]*int data type, the rvalue of assignment will escape.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
Among them moved to heap: a, the variable a is finally moved to the heap.
- Escape example six
func(*int) function type, function assignment will cause the passed formal parameters to escape.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We will see that the data has been escaped to the heap.
- Escape example seven
func([]string): function type, assigning []string{“value”} will cause the passed parameters to escape.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We see that s escapes to heap, s is escaped to the heap.
- Escape Example 8
chan []string data type, if you want to transmit []string{“value”} in the current channel, escape will occur.
1 | package main |
Let’s take a look at the escape results through compilation
1 | go build -gcflags=-m main.go |
We see that []string{…} escapes to heap, s is escaped to the heap.