当前位置:网站首页>Escape analysis of 982 golang
Escape analysis of 982 golang
2022-06-25 07:12:00 【Python's path to becoming a God】
Preface
So called escape analysis (Escape analysis) It refers to the location of memory allocation determined by the compiler , There is no need for programmers to specify . Function to request a new object
If allocated on the stack , After the function is executed, the memory can be recycled automatically ;
If the allocation is in the heap , Then the function can be handed over to GC( Garbage collection ) Handle ;
With escape analysis , Return function local variables will become possible , besides , Escape analysis is also closely related to closures , Know which scenes the object will escape crucial .
Escape strategy
Whenever a new object is requested in the function , The compiler will decide whether to escape according to whether the object is externally referenced by the function :
- If there is no reference outside the function , Is placed on the stack first ;
- If there is a reference outside the function , It must be in the heap ;
Be careful , For objects not referenced outside the function , It's also possible to put it in the heap , For example, the memory is too large and exceeds the storage capacity of the stack .
Escape scene
The pointer escaped
We know Go You can return a pointer to a local variable , This is actually a typical case of variable escape , The sample code is as follows :

function StudentRegister() Inside s Is a local variable , Its value is returned by the return value of the function ,s Itself is a pointer , The memory address it points to will not be Stack but pile , This is a typical escape case .
By compiling parameters -gcflag=-m You can check the escape analysis in the compilation process :
Visible in StudentRegister() Function , I.e. code No 9 Row display ”escapes to heap”, Indicates that the memory allocation of this line has escaped like .
Insufficient stack space to escape
Look at the code below , Whether there will be escape ?
Above code Slice() A... Is assigned in the function 1000 Slices of length , Whether to escape depends on whether the stack space is large enough . Directly view the compilation tips , Such as Next :

We found no escape here . Then expand the length of the slice 10 Double is 10000 How will? ?
We found that when the slice length expanded to 10000 You'll run away .
In fact, when the stack space is insufficient to store the current object or the current slice length cannot be determined, the object will be allocated to the heap .
Dynamic type escape
Many function parameters are interface type , such as fmt.Println(a …interface{}), It is difficult to determine the specific type of its parameters during compilation , People also escape . As shown in the following code :

The above code s Variable is just a string Type variable , call fmt.Println() There will be escape :
Closure reference object escape
A famous open source framework implements a return Fibonacci A function of a sequence :
This function returns a closure , The closure refers to the local variable of the function a and b, Use this function to obtain the closure , Then each time the closure is executed, it will be in turn Output Fibonacci The sequence . The complete sample program is as follows :


The above code is passed Fibonacci() Get a closure , Each time the closure is executed, a Fibonacci The number . The output is as follows :
Fibonacci() Functions that were originally local variables a and b Due to the reference of closure , I have to put them on the pile , So as to escape :
Escape summary
Allocating memory on the stack is more efficient than allocating memory in the heap
Memory allocated on the stack is not required GC Handle
The memory allocated on the heap will be handed over to GC Handle
The purpose of escape analysis is to determine whether the internal allocation address is stack or heap
Escape analysis is completed in the compilation phase
Think about it
Is function transfer pointer really more efficient than value transfer ?
We know that passing pointers can reduce the copy of underlying values , Can improve efficiency , But if the amount of data copied is small , Due to the pointer passing, there will be escape , The heap may be used , It may also increase GC The burden of , So passing a pointer doesn't have to be It's efficient .
边栏推荐
- Why use NoSQL with MySQL?
- [C language] add separator to string
- Keil debug view variable prompt not in scope
- Clearing Magento log data - clearing Magento log data
- keil debug查看变量提示not in scope
- How to store the directory / hierarchy / tree structure in the database- How to store directory / hierarchy / tree structure in the database?
- [ACNOI2022]王校长的构造
- Query JSON data in MySQL table
- Derivation of sin (a+b) =sina*cosb+sinb*cosa
- 单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
猜你喜欢

joda. Time get date summary

Hongmeng learning notes: creating layouts using XML
![[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met](/img/fe/864e9d91be2e0afb163cb8496ae0d2.png)
[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met

【xxl-job】池塘水绿风微暖,记得玉真初见面

CTFHub-Web-信息泄露-目录遍历

レ / leilei

What is the real future of hardware engineers?

Are you still doing the dishes yourself? Teach you how to make dishwasher controller with single chip microcomputer

分布式锁中的王者方案 - Redisson

基于 KubeSphere 的分级管理实践
随机推荐
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
Hongmeng learning notes: creating layouts using XML
Error reported during vivado simulation common 17-39
Derivation of COS (a+b) =cosa*cosb-sina*sinb
Derivation of COS (a-b) =cosa*cosb+sina*sinb
Navicat prevent new query from being deleted by mistake
shell 上下两行合并成一行
[tool sharing] a software that pays equal attention to appearance and skills
Efficient exploration | an application practice of ES geographical location query
Derivation of sin (a+b) =sina*cosb+sinb*cosa
48 张图 | 手摸手教你微服务的性能监控、压测和调优
Leetcode 2163. Minimum difference of sum after element deletion
Changing the background color of tab bar - changing the background color of tab bar
Expression of fatherly love
Blue Bridge Cup SCM module code (timer) (code + comments)
网络是怎样连接的?
Capable people never complain about the environment!
Shell command learning
[he doesn't mention love, but every word is love]
[ACNOI2022]王校长的构造