当前位置:网站首页>js garbage collection mechanism
js garbage collection mechanism
2022-08-03 04:32:00 【weixin_46051260】
The garbage collection mechanism is called Garbage Collection for short GC.The garbage collection mechanism of js is a mechanism to periodically recycle idle resources. Every once in a while, the execution environment will clean up some useless variables in the memory to release the memory occupied by him.
Core idea: find useless variables and free their memory
1) Tag Clear
Tag removal is the most commonly used garbage collection strategy. Browsers that use tag removal as a garbage collection mechanism will perform the following steps during the garbage collection process:
- All variables in the standard memory
- Remove the variables declared in the context (global scope, script scope) and the tags of the variables that are referenced in the global, and all the remaining marked variables are regarded as variables to be deleted, garbageFree the memory they occupy when reclaiming executes
- Clean up memory, remove junk
// The variables color and dog are declared in the global environment and will not be clearedconst color = 'red';var dog = 'Golden Retriever';{let cat = 'kitty'; // The variable cat is declared in block scope and is not globally referenced, so its memory will be released when the next garbage collection is performedspan>}
2) Reference counting
Reference counting is an uncommon garbage collection strategy. The main core idea is to record the number of times a value is referenced. A value is assigned to a variable, the number of references +1, and this variable is reassigned a new value at a certain time., the reference count of the old value -1 becomes 0, and its memory will be released the next time the garbage collection process is performed.
The problem with citation technology: circular references
function fn() {const obj1 = new Object()// new Object creates an object 1 in heap memory {} this value is assigned to obj1 so the number of references + 1const obj2 = new Object()// new Object creates an object 2 in heap memory {} this value is assigned to obj2 so the number of references + 1obj1.a = obj2; // obj2 is assigned to the a property of obj1, so the number of references to object 1 is 1+1 = 2obj2.a = obj1; // obj1 is assigned to the a property of obj2, so the number of references to object 2 is 1+1 = 2}// At this time, the two objects refer to each other. If the function is called multiple times, the function body will be re-executed multiple times, and there will be n more objects that refer to each other occupying memoryspan>
obj1.a = null;obj2.a = null;// By setting it to null, the reference between the two can be cut off, and it will be cleaned up and released in the next recycling
边栏推荐
猜你喜欢
随机推荐
私域流量引流方法?分享购火爆的商业模式,你值得拥有
RequestContextHolder
肖sir___面试就业课程____性能测试
WinForm的控件二次开发
js中的闭包
接口测试如何准备测试数据
【uni-APP搭建项目】
DC-3靶场搭建及渗透实战详细过程(DC靶场系列)
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
移动流量的爆发式增长,社交电商如何选择商业模式
工程制图点的投影练习
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
计组错题集
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
索引创建、删除与使用
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
StarRocks 7 月社区动态
Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?