go stack object escape

Posted dy blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go stack object escape相关的知识,希望对你有一定的参考价值。

官方说明

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.

 

参考资料/blog:

Golang逃逸分析

Go Escape Analysis Flaws
go-escape-analysis

 

以上是关于go stack object escape的主要内容,如果未能解决你的问题,请参考以下文章

Go语言栈定义及相关方法实现

ios学习路线—Objective-C(堆(heap)和栈(stack))

栈类Stack

Creating objects on stack or heap

细谈Go变量的内存分布

细谈Go变量的内存分布