当前位置:网站首页>内存逃逸分析
内存逃逸分析
2022-06-30 10:03:00 【fpcc】
一、内存管理
内存管理其实有好多种方法,但从发展历程来看,一般是从开发者的自主管理(手动)内存到相关程序的自动管理(自动)内存。手动管理的内存优点在于可以随时管控相关的内存的状态并进行处理,但缺点是,人自主行为的不可控,特别是可能会忘记内存的回收,这就引起内存泄露。而自动管理内存就很少出现这个问题,因为GC可以定时定或者被动的来进行内存垃圾的回收。但它也有不少问题,比如可能造成程序暂停,效率低等等。
在GC过程中,内存的处理机制不同就会引出今天就分析的问题,也就是内存逃逸。
二、内存逃逸
在手动内存分配中,内存指定分配在哪儿就是在哪儿。但是在自动管理内存的带GC的程序中,事情发生了一些变化。编译器会针对具体的情况来对对象(变量)的内存分配的位置进行调整,这个过程就叫做内存逃逸。说得再简单一些,就是如果有一些对象可能分配在栈上,但编译器发现其可能被其它情况覆盖,因此将其调整到堆上。这样,其生存周期会增长。反之,从堆到栈也是如此。看一个例子:
import "fmt"
func Test(ID int) int {
var curID int
curID = ID
return curID
}
func main() {
var ID int
fmt.Println(ID, Test(0))
}
在Goland的编译参数里增加 -gcflags "-m -l”或者直接在终端内输入命令“go run -gcflags “-m -l” main.go”
运行结果为:
# 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
可以看到相关的内存逃逸进行了说明,有兴趣可以多根据实际情况进行测试一下例子。
三、逃逸分析
下面来分析一下具体的逃逸的情况,首先确定一下逃逸判定的规则:
1、假如函数外部没有引用,则优先放到栈中;
2、假如函数外部存在引用,则放到堆中;
3、假如栈上容纳不下,则会放到堆上
再看一下逃逸的具体场景:
1、指针逃逸
2、栈空间不足逃逸
3、动态类型逃逸
4、闭包引用对象逃逸
一般来说,栈上分配空间比在堆上分配效率高也更安全,而且栈的内存是系统自动管理不需要GC,而堆上分配的内存需要GC,而在前面分析过,所谓逃逸,其实就是在编译阶段分析是否进行堆栈内存转换。
而在实际的应用过程,内存逃逸又有一些比较典型的例子:比如函数返回一个局部变量的指针;把指针或含指针的数据发送到通道;切片上存储指针或者含指针的对象;切片底层数组的扩大分配;在接口类型上调用方法;栈空间不足;对map赋值指针类型对象等等。
那么内存逃逸有什么作用呢?
1、内存逃逸的可以减轻GC的压力,只有逃逸到堆上的才GC。
2、内存逃逸分析可以明确变量的位置并确定效率。
3、对线程而言,可以减轻同步复杂度提高运行效率。经过逃逸分析后,假如不会多线程操作会自动取消线程中的锁和相关同步机制。
四、总结
做计算机软件的,其实不外乎几个问题。CPU(多线程、多进程等)、内存(小代价大结果)、IO(尽可能速度、安全等),网络通信(分布式等),不是说其它的问题不是问题,而是最主要的就是这么几个,这里面最让人头疼不已的,内存管理这个问题肯定能拔个头筹(不认可也没办法)。其实写代码写多了,你就会发现,内存才是第一大敌人,其它都有时有晌,唯有内存的问题,常伴在身畔。
想把代码写得健壮,还是要认真看看内存如何管理好,特别是自己写代码时。不要认为有了GC,天下太平,否则,会很难看。
边栏推荐
- Questions about cookies and sessions
- Collectors.toMap应用
- 最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了
- Deployment of efficient and versatile clusters lvs+kept highly available clusters
- nvm、nrm、npx使用(安装、基本命令、参数、curl、wget)
- 超长干货 | Kubernetes命名空间详解
- 机器学习面试准备(一)KNN
- MySQL advanced SQL statement of database (2)
- CSDN博客运营团队2022年H1总结
- Compétences Comb 27 @ Body sense Manipulator
猜你喜欢

The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if

Why can't you rob scientists of NFT

2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
[email protected]體感機械臂"/>技能梳理[email protected]體感機械臂

Kernel linked list (general linked list) "list.h" simple version and individual comments

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
[email protected] somatosensory manipulator"/>Skill combing [email protected] somatosensory manipulator

6. Redis new data type

mysql数据库基础:视图、变量

CSDN博客运营团队2022年H1总结
随机推荐
mysql数据库基础:存储过程和函数
【Rust日报】2021-01-23 几个新库发布
Notes on numerical calculation - iterative solution of linear equations
Enter the world of helium (hNT) hotspot servers to bring you different benefits
Auto SEG loss: automatic loss function design
GeoffreyHinton:我的五十年深度学习生涯与研究心法
JS FAQs
MySQL index, transaction and storage engine of database (1)
ArcGIS Pro + PS 矢量化用地规划图
Auto Seg-Loss: 自动损失函数设计
Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled
Viewing technological changes through Huawei Corps (V): smart Park
潘多拉 IOT 开发板学习(HAL 库)—— 实验1 跑马灯(RGB)实验(学习笔记)
Ionic4 drag the ion reorder group component to change the item order
Skill combing [email protected] voice module +stm32+nfc
The programmer was beaten.
Compétences Comb 27 @ Body sense Manipulator
Using LVM to resize partitions
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
如何解决跨域