当前位置:网站首页>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 .
边栏推荐
- #23class介绍
- Leetcode (605) -- flower planting
- Leetcode(605)——种花问题
- Use and difference between ue4\ue5 blueprint node delay and retroggable delay
- UE4_UE5结合offline voice recognition插件做语音识别功能
- 【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
- Ble Low Power Bluetooth networking process and Bluetooth role introduction
- CSDN force value
- UE4_UE5結合offline voice recognition插件做語音識別功能
- 问题记录与思考
猜你喜欢

【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域

Repoptimizer: it's actually repvgg2

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千

Meta universe standard forum established

Once beego failed to find bee after passing the go get command Exe's pit

#23class介绍

记一次beego通过go get命令后找不到bee.exe的坑

konva系列教程2:绘制图形

Pointer strengthening and improvement

What is Unified Extensible Firmware Interface (UEFI)?
随机推荐
Hbuilderx uses the gaude map to obtain the current location
How to use drawing comparison function in CAD
PDM fur
24class static member
Ad20 learning notes I
【opencv450-samples】读取图像路径列表并保持比例显示
指针强化与提高
qtcreator 格式化代码
Day4 branch and loop summary and operation
Leetcode(605)——种花问题
Leetcode(435)——无重叠区间
解决‘tuple‘ object has no attribute ‘lower‘
Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
记一次beego通过go get命令后找不到bee.exe的坑
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
CSDN添加页内跳转和页外指定段落跳转
Basic operator
记录一下Qt将少量图片输出为MP4的思路及注意事项
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
【AXI】解读AXI协议乱序机制