当前位置:网站首页>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 .
边栏推荐
- 记录一下Qt将少量图片输出为MP4的思路及注意事项
- 库项目和App项目中清单文件的包名不要相同
- 【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
- 信息学奥赛一本通 1353:表达式括号匹配(stack) | 洛谷 P1739 表达式括号匹配
- How to use drawing comparison function in CAD
- Day4 branch and loop summary and operation
- Once beego failed to find bee after passing the go get command Exe's pit
- Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
- hiberate实体类CURD、事务操作汇总
- User interaction scanner usage Advanced Edition example
猜你喜欢

23class introduction

#23class介绍

When are the three tools used for interface testing?

Fegin client entry test

Idea FAQ collection

#24class静态成员

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

Pycharm student's qualification expires, prompting no suitable licenses associated with account solution

jdbc常见异常及错误解决办法汇总

【AXI】解读AXI协议原子化访问
随机推荐
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
#24class静态成员
漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案
My vscode
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood
Problem recording and thinking
经典图像分割网络:Unet 支持libtorch部署推理【附代码】
My C language learning process
Comp2913 database
转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)
Multithreaded learning 2- call control
konva系列教程2:绘制图形
做接口测试,这3种工具到底什么时候用?
RK3568+鸿蒙工控板工业网关视频网关解决方案
User interaction scanner usage Advanced Edition example
Uni app -- listen for the exit of the return key
C language (I)
Ad20 learning notes II
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现