当前位置:网站首页>Garbage collection mechanism
Garbage collection mechanism
2022-04-21 08:47:00 【Google player】
Garbage collection mechanism
The concept of garbage collection
JS Code runtime , You need to allocate memory space to store variables and values , When the variable no longer participates in the run , The system needs to recover the occupied memory space .
Recycling mechanism
- JS With automatic recycling mechanism , Variables that are no longer used are periodically 、 The memory occupied by the object is released , The principle is to find variables that are no longer used , And then release the memory it's using .
- JS There are global variables and local variables . Global variables will always be stored in memory , Variable memory is not reclaimed until the page is unloaded ; Local variables are declared inside the function , The memory will be reclaimed after the function is executed .
- When using closures , The local variables defined inside the function will remain in memory , Will not be used . So try to avoid using closures , To avoid memory leakage .
Garbage collection way
- Mark clear :
- When variables are executed in the environment , Just mark this variable
Enter the environment, Marked asEnter the illusionThe variable of is not recyclable , Because they are being used . When a variable leaves the environment , Will be marked asLeaving the environment, Marked asLeaving the environmentThe variable will be released by memory . - At runtime, the garbage collector marks all variables stored in memory . then , It removes variables in the environment and tags referenced by variables in the environment , The remaining variables will be treated as variables that need to be deleted , The garbage collector completes the memory cleanup , Destroy the marked values and reclaim the memory they occupy .
- Reference count :
- Reference count is to track the number of times each value is referenced , When a variable is declared and a reference type is assigned to it , Then the number of references to this value is 1; contrary , If the variable that contains this value reference gets another value , The number of references to this value is reduced 1. When the number of references becomes 0, It means that this variable is useless , The next time the garbage collector runs, it will reclaim memory .
- This method will cause Circular reference problem , for example :
function fun(){
let obj1 = {
}
let obj2 = {
}
obj1.a = obj2 // obj1 quote obj2
obj2.a = obj1 // obj2 quote obj1
}
In the example above ,obj1 and obj2 Cross reference , The number of references to both objects is 2. When the function is finished , Two objects leave the scope , but obj1 and obj2 The number of citations is still 2, Will not be reduced to 0, So it won't be recycled .
The solution is : Manual memory release .
obj1.a = null
obj2.a = null
Reduce garbage collection
Although browsers can do automatic garbage collection , But when the code is complex , The cost of garbage collection is relatively high , So we should try to reduce garbage collection .
- Optimize the array : When emptying an array , Assign it to
[] - Yes object To optimize : Objects should be reused as much as possible , For objects that are no longer used , The assignment is
null - Optimize the function : In the loop
Function expression, If it can be reused , Try to put it outside the function
What can cause memory leaks
- Unexpected global variables : If a variable is not declared , Will accidentally create a global variable , This will keep the variable in memory and cannot be recycled
- Forgotten timer or callback function : Set up setInterval And forget to cancel it , If the loop function has a reference to an external variable , Then this variable will be left in memory all the time , And can't be recycled
- Closure : Unreasonable use of closures , Will cause the variable to remain in memory
- Out of the DOM References to : Got a DOM Reference to element , And the latter element is deleted , Since the reference to this element has been retained , So it can't be recycled
版权声明
本文为[Google player]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210836125807.html
边栏推荐
- 关于印发《湖南省首版次软件产品认定管理办法》的通知
- 受控组件与非受控组件
- 卷积运算与互相关运算
- 多线程小抄集(新编四)
- Power grid enterprise standard B interface access record (II): resource reporting
- Overview of VoIP technology development and its relationship with outbound call system
- JS原型与原型链
- ffmpeg整合sdl2实现纹理渲染随机出现的方块
- 2022 mobile crane driver examination exercises simulated examination platform operation
- uniapp 热更新和整包更新
猜你喜欢

2022年上海市安全员C证考试模拟100题及模拟考试

ZABBIX 5.4 server installation
![[reprint] explain Fourier transform in simple terms](/img/1d/69fb2589ec77df83869d7cd1fdabc2.jpg)
[reprint] explain Fourier transform in simple terms

Characteristics of interactive multimedia applications

鸢尾花种类预测--数据集介绍

JVM——》常用参数

阿里巴巴温少再度出山重构fastjson推出fastjson2

2022 chemical automation control instrument examination exercises and simulation examination

机器学习笔记 - SVD奇异值分解(2)

渗透实战-挖掘某学校站点漏洞(APP漏洞)
随机推荐
51 单片机学习_1.2 LED闪烁
鸢尾花种类预测--数据集介绍
[ctf.show.reverse] 逆向AK赛 EasyDSE
2022 chemical automation control instrument examination exercises and simulation examination
Explain in simple terms the optimization of ext4 block and inode distributor (Part 2)
[GYCTF2020]Blacklist
What is the product power of the new modern paristi, a joint venture 7-seat SUV with large displacement?
User authentication center of micro service
Enter four integers in descending order
C语言计数排序
[tools] summary of library functions for remote interaction in PWN
L2-026 小字辈 (25 分)
51 单片机学习_1.1 点亮一个LED
淘宝小程序体验优化:数据分析和优化实践
Power grid enterprise standard B interface access record (II): resource reporting
JVM——》G1垃圾收集器
36氪首发|「甄知科技」收购数智化开发平台「猪齿鱼」,将和已有产品「燕千云」融合形成产品闭环
受控组件与非受控组件
机器学习笔记 - SVD奇异值分解(2)
渗透测试-从公有云到内网漫游RCE-反序列化-frp