当前位置:网站首页>Go language escape analysis complete record
Go language escape analysis complete record
2022-06-25 23:35:00 【_ Qilixiang】
Catalog
2, Insufficient stack space to escape
3, Dynamically allocate escape
4, Closure reference object escape
What is escape analysis ?
Go The program allocates memory for variables in two ways :
1, The global heap space dynamically allocates memory
2, Every goroutine Stack space of In general, developers don't need to worry about memory allocation on the stack or Or on the pile . But from a performance standpoint , Allocate memory on the stack and on the heap , The performance difference is still very large .
The overhead of allocating and reclaiming memory on the stack is very low , It only needs 2 individual CPU Instructions :PUSH、POP.
The former is to transfer data push To stack space to complete allocation , The latter is to free up space , That is, allocate memory on the stack , All it takes is the time to copy the data to memory , And on the heap , A big extra cost is garbage collection .
stay Go in , Heap memory is automatically managed through garbage collection ,Go Our garbage collection uses a mark removal algorithm , On this basis, three color marking method and write barrier technology are used , Improved efficiency .
A typical operation of a mark removal algorithm is during marking , need STW, That is, suspend the program (Stop the world), After marking , The user program can continue to execute .
Heap memory allocation causes garbage collection to cost much more than stack space allocation and release .
that Go How does the compiler know that a variable needs to be allocated on the stack or On the pile ?
How the compiler determines where memory is allocated , It's called escape analysis (escape analysis). Escape analysis is done by the compiler , Works in the compilation phase .
What is escape analysis for ?
go Escape analysis will be carried out during compilation , The purpose is to decide whether an object is put on the stack or on the heap , Objects that don't escape are put on the stack , Put it on the pile that may escape .
The biggest benefit is to reduce gc The pressure of the , Non escaping objects are allocated on the stack , When the function returns, it reclaims resources , Unwanted gc Mark clear .
Because escape analysis can determine which variables can be allocated on the stack , Stack allocation is faster than heap , Good performance .
How to view escape
compile go Code time plus
-gcflags "-m -l"Parameters can be .
What would happen to escape
1, The pointer escaped
func f(x, y int) *int {
n := new(int) // new(int) escapes to heap
*n = x * y
return n
}
_ = f(10, 20) In the above code, a pointer is returned at the end of the function , Compile now ,new(int) Will result in variables n Escape to the pile .
here n As function f The return value of will be in main Continue to use in , therefore n The memory pointed to cannot be allocated on the stack , Will be recycled as the function ends .
2, Insufficient stack space to escape
_ = make([]int, 1000, 8191) // make([]int, 1000, 8191) does not escape here <64KB (int Occupy 8 byte )
_ = make([]int, 1000, 8193) // make([]int, 1000, 8193) escapes to heap here >64KB
_ = make([]int, 8193) // make([]int, 8193) escapes to heap
_ = make([]int, 1000, 10000) // make([]int, 1000, 10000) escapes to heap in other words , Every time you make Create different lengths , In fact, compilers do different things , Different situations may lead to higher overhead ;
When slices occupy more than a certain amount of memory Or when the current slice length cannot be determined , Its memory will be allocated on the heap .
How much of it will escape to the heap is limited by the size of the kernel thread stack space by the operating system .
3, Dynamically allocate escape
func f1() {
s := 10
_ = make([]int, s) // make([]int, s) escapes to heap
}
f1() // Because of the variable s May be changed , So the compiler thinks it should be assigned to heapOther cases, such as dynamic types , The parameter for interface{}, The compiler cannot determine what type it is , There will also be escape .
It's like fmt.Printf(), It has a lot of deception .
4, Closure reference object escape
Go The language supports closure mechanism , as follows :
func f2() func() int {
a, b := 1, 2
return func() int { // func literal escapes to heap
return a + b
}
}
f2() Originally a and b As a function, local variables should be assigned to stack in , But because of f() Function returns a closure function , The closure function accesses the external variables a and b, At this point, if the function f2 return, actual a and b Or is it quoted , It can't be recycled , So the compiler thinks a and b Should be allocated to the heap .
边栏推荐
- PDM fur
- 指针强化与提高
- #24class静态成员
- Recently prepared to translate foreign high-quality articles
- Implementation of importing vscode from PDM
- 统计字符串中不同回文子序列的个数
- 分享一个OSGeo4W64下载好的库,基于qgis3.10的
- LeetCode-1528-重新排列字符串-哈希表-字符串
- Svn icon disappearing solution
- [untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
猜你喜欢

Set up your own website (15)

24class static member

转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)

UE4 学习记录二 给角色添加骨架,皮肤,及运动动画

Uni app -- listen for the exit of the return key

毕业旅行 | 伦敦5日游行程推荐

Pointer strengthening and improvement

My vscode

When are the three tools used for interface testing?

Why is the frame rate calculated by opencv wrong?
随机推荐
分享一个OSGeo4W64下载好的库,基于qgis3.10的
Konva series tutorial 2: drawing graphics
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
jdbc常见异常及错误解决办法汇总
Implementation of sequence table: static and dynamic
A. Balance the Bits--Codeforces Round #712 (Div. 1)
Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
character string
【无标题】打开一个项目连接,无法正常显示时,ping一下ip
Idea FAQ collection
字符串
CSDN force value
What is Unified Extensible Firmware Interface (UEFI)?
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
问题记录与思考
CSDN add on page Jump and off page specified paragraph jump
库项目和App项目中清单文件的包名不要相同
关于go中资源泄漏/goroutine泄漏/内存泄漏/CPU打满等情况分析
Idea shortcut