当前位置:网站首页>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 .
边栏推荐
- Auto Seg-Loss: 自动损失函数设计
- GD32 RT-Thread OTA/Bootloader驱动函数
- Remember the experience of an internship. It is necessary to go to the pit (I)
- Compétences Comb 27 @ Body sense Manipulator
- Jinbei LT6 is powerful in the year of the tiger, making waves
- Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
- 断路器HystrixCircuitBreaker
- WGet -- 404 not found due to spaces in URL
- [deep learning] common methods for deep learning to detect small targets
- Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
猜你喜欢

最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了

Voir le changement technologique à travers la Légion Huawei (5): Smart Park

Matplotlib notes: contour & Contour

Machine learning interview preparation (I) KNN

How to deploy deflationary combustion destruction contract code in BSC chain_ Deploy dividend and marketing wallet contract code

WGet -- 404 not found due to spaces in URL

同事的接口文档我每次看着就头大,毛病多多。。。
[email protected]+adxl345+电机震动+串口输出"/>技能梳理[email protected]+adxl345+电机震动+串口输出

Criu enables hot migration

从0使用keil5软件仿真调试GD32F305
随机推荐
【Proteus仿真】Arduino UNO LED模拟交通灯
sCrypt 中的 ECDSA 签名验证
SGD有多种改进的形式,为什么大多数论文中仍然用SGD?
Jinbei LT6 is powerful in the year of the tiger, making waves
Notes on numerical calculation - iterative solution of linear equations
IPhone address book import into Excel
The programmer was beaten.
"Kunming City coffee map" was opened again, and coffee brought the city closer
ArcGIS Pro + PS 矢量化用地规划图
Overview of currency
TypeScript–es5中的类,继承,静态方法
Skill combing [email protected] intelligent instrument teaching aids based on 51 series single chip microcomputer
技能梳理[email protected]语音模块+stm32+nfc
Review of mathematical knowledge: curve integral of the second type
ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources
Q-Learning笔记
[rust weekly database] num bigint - large integer
安徽《合肥市装配式建筑施工图审查设计深度要求》印发;河北衡水市调整装配式建筑预售许可标准
同事的接口文档我每次看着就头大,毛病多多。。。
Collectors.toMap应用