“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”
What is memory escape?
Without further ado, load up.
Their roots
There’s always something new to discover while you’re learning. We all know that when we create an object, we automatically allocate an area of memory to hold the object instance. So you’ve heard of itMemory escapeThe idea?Who made the escape? Where did he go again?Take a look.
What is memory escape
Memory escape is when a variable is allocated in memory, the compiler performs escape analysis based on the calls to objects, variables, and methods. Memory escape phenomenon occurs in many languages, similar to Java will occur in the JVM memory escape, including method escape and thread escape, also in go language memory escape phenomenon occurs, today we take GO as an example to say that memory escape.
Memory allocation generally has two cases, one is allocated on the stack, one is allocated on the heap. Memory escape is when memory allocation is completed, and the compiler performs escape analysis based on object, variable, and method calls. Memory allocation changes, such as from stack to heap, or from heap to stack. Text after all slightly empty, on the code
Write a demo
package main
import "fmt"
func demo(user int) int {
// Name a variable and assign a value
var admin int
admin = user
return admin
}
// The empty method is useless
func void(a){}func main(a) {
// Declare the user variable and print
var user int
void()
fmt.Println(user, demo(0))}Copy the code
Then run the above code using the following command line:
go run -gcflags "-m -l" main.go
Copy the code
When running the program with Go Run, the -gcflags argument is the compile argument. -m indicates to perform memory allocation analysis, and -l indicates to avoid program inlining, that is, avoid program optimization.
The results
# command-line-arguments
./main.go:29:13: user escapes to heap
./main.go:29:22: demo(0) escapes to heap
./main.go:29:13: main ... argument does not escape
0 0
Copy the code
./main.go:29:13: A escapes to the heap./main.go:29:22: ESCAPES to the heap Demo (0) escapes to heap. Likewise, Demo (0) escapes to the heap
Escape analysis
Now, what are the rules for the compiler to determine escape? Quote a passage that appears to be from an official translation
If a function returns a reference to a variable, it will escape. Any time a value is shared out of range of function stack frames, it is redistributed on the heap. If, after the function return, it is determined that the variable is no longer referenced, it is assigned to the stack. However, if the compiler cannot ensure that the variable is no longer referenced after the function return, the compiler allocates the variable to the heap. Also, if a local variable is very large, it should also be assigned to the heap rather than the stack.
To summarize, if there is no reference outside the function, it is placed on the stack first; If a reference exists outside the function, it must be placed in the heap; If you can’t get it off the stack, you must put it on the heap
Common scenario
- Returning a pointer to a local variable within a method is supposed to be allocated on the stack and recycled on the stack. But because it is referenced externally when returned, its lifetime is longer than the stack, then it overflows.
- Stores Pointers or values with Pointers on a slice. A typical example is []*string. This causes the contents of the slice to escape. Although the array behind it may be allocated on the stack, the value it references must be on the heap.
- The array behind Slice is reallocated because it may exceed its capacity (CAP) at append. The initialization of slice is known at compile time and is initially allocated on the stack. If the storage behind the slice is to be expanded based on runtime data, it is allocated on the heap.
- Call a method on the interface type. Methods called on the interface type are dynamically scheduled — the true implementation of the method is known only at run time.
Finally, I’d like to recommend a book to you
If you have any mistakes or need to add please write below and fight with me!Good thanks ~