当前位置:网站首页>Memory escape analysis
Memory escape analysis
2022-06-30 10:44:00 【fpcc】
One 、 memory management
There are many ways to manage memory , But from the perspective of the development process , Generally, it is from the independent management of developers ( Manual ) Automatic management of memory to related programs ( Automatically ) Memory . The advantage of manual memory management is that it can control and process the related memory status at any time , But the disadvantage is , The uncontrollable behavior of human beings , In particular, you may forget to recycle memory , This causes a memory leak . This problem rarely occurs in automatic memory management , because GC The memory garbage can be collected regularly or passively . But it also has many problems , For example, the program may be suspended , Low efficiency and so on .
stay GC In the process , Different memory processing mechanisms will lead to the problems analyzed today , Memory escape .
Two 、 Memory escape
In manual memory allocation , Memory specifies where to allocate it . But in the automatic memory management band GC In the program , Something has changed . The compiler will modify the object according to the specific situation ( Variable ) The location of the memory allocation , This process is called memory escape . Make it simpler , If there are some objects that may be allocated on the stack , But the compiler found that it might be overwritten by something else , So adjust it to the heap . such , Its life cycle will grow . conversely , The same is true from heap to stack . Take an example :
import "fmt"
func Test(ID int) int {
var curID int
curID = ID
return curID
}
func main() {
var ID int
fmt.Println(ID, Test(0))
}
stay Goland Add... To the compilation parameters of -gcflags "-m -l” Or directly input commands in the terminal “go run -gcflags “-m -l” main.go”
The running result is :
# command-line-arguments
.\main.go:25:13: ... argument does not escape
.\main.go:25:13: ID escapes to heap
.\main.go:25:22: Test(0) escapes to heap
You can see that the related memory escapes are explained , If you are interested, you can test some examples according to the actual situation .
3、 ... and 、 Escape analysis
Let's analyze the specific situation of escape , First of all, determine the rules of escape judgment :
1、 If there is no reference outside the function , Is placed on the stack first ;
2、 If there is a reference outside the function , Put it in the heap ;
3、 If there is no room on the stack , Will be put on the heap
Let's take a look at the specific scene of the escape :
1、 The pointer escaped
2、 Insufficient stack space to escape
3、 Dynamic type escape
4、 Closure reference object escape
Generally speaking , Allocating space on the stack is more efficient and safer than allocating space on the heap , And the memory of the stack is automatically managed by the system, so it is unnecessary GC, The memory allocated on the heap requires GC, But I have analyzed it before , The so-called escape , In fact, it is to analyze whether stack memory conversion is performed at the compilation stage .
But in the actual application process , There are some typical examples of memory escape : For example, a function returns a pointer to a local variable ; To send a pointer or data containing a pointer to a channel ; Storing pointers or objects containing pointers on slices ; Expanded allocation of the underlying array of slices ; Call method on interface type ; Not enough stack space ; Yes map Assign pointer type objects, etc .
So what does memory escape do ?
1、 Memory escape can be reduced GC The pressure of the , Only those who escape to the heap GC.
2、 Memory escape analysis can identify the location of variables and determine efficiency .
3、 For threads , It can reduce the synchronization complexity and improve the operation efficiency . After escape analysis , If there is no multi-threaded operation, the lock and related synchronization mechanism in the thread will be automatically cancelled .
Four 、 summary
Doing computer software , In fact, it is nothing more than a few questions .CPU( Multithreading 、 Multi process, etc )、 Memory ( Small cost, big result )、IO( As fast as possible 、 Safety, etc ), Network communication ( Distributed, etc ), Not that other problems are not problems , But the most important ones are these , Among them, the most annoying thing , Memory management is definitely a top priority ( I can't help it if I don't recognize it ). In fact, I wrote too much code , You'll find out , Memory is the first enemy , Others sometimes have a day , The only problem is memory , Always with me .
Want to write robust code , We should take a careful look at how to manage the memory , Especially when you write your own code . Don't think you have GC, All is well , otherwise , It's going to be ugly .
边栏推荐
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验1 跑马灯(RGB)实验(学习笔记)
- Skill combing [email protected] voice module +stm32+nfc
- 无心剑中译狄金森《灵魂择其伴侣》
- RobotFramework学习笔记:环境安装以及robotframework-browser插件的安装
- Migrate full RT thread to gd32f4xx (detailed)
- The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
- Matplotlib notes: contour & Contour
- Mysql database foundation: views and variables
- Agile Development: super easy to use bucket estimation system
- Typescript – classes in Es5, inheritance, static methods
猜你喜欢
CVPR 2022 | Tsinghua & bytek & JD put forward BRT: Bridging Transformer for vision and point cloud 3D target detection
苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏
The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if
CSDN blog operation team 2022 H1 summary
mysql数据库基础:存储过程和函数
Voir le changement technologique à travers la Légion Huawei (5): Smart Park
GeoffreyHinton:我的五十年深度学习生涯与研究心法
Pytorch Notebook. Nn. Batchnorm1d
Auto Seg-Loss: 自动损失函数设计
Go -- maximum heap and minimum heap
随机推荐
Dow Jones Industrial Average
June training (day 30) - topology sorting
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
Jinbei LT6 is powerful in the year of the tiger, making waves
mysql数据库基础:存储过程和函数
转卡通学习笔记
无心剑中译狄金森《灵魂择其伴侣》
The rising star of Goldshell STC box
RobotFramework学习笔记:环境安装以及robotframework-browser插件的安装
Agile Development: super easy to use bucket estimation system
05_ Node JS file management module FS
Migrate full RT thread to gd32f4xx (detailed)
[rust daily] several new libraries were released on January 23, 2021
ionic4 ion-reorder-group组件拖拽改变item顺序
Compétences Comb 27 @ Body sense Manipulator
CSDN博客运营团队2022年H1总结
Gd32 RT thread ota/bootloader driver function
Review of mathematical knowledge: curve integral of the second type
Musk has more than 100 million twitter fans, but he has been lost online for a week
The programmer was beaten.