码迷,mamicode.com
首页 > 其他好文 > 详细

go stack object escape

时间:2018-01-15 17:36:01      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:relevant   cannot   faq   address   blank   ble   ret   collect   eva   

官方说明

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

标签:relevant   cannot   faq   address   blank   ble   ret   collect   eva   

原文地址:https://www.cnblogs.com/cdyboke/p/8288837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!