当前位置:网站首页>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
边栏推荐
- 表的创建、修改与删除
- IO进程线程->线程->day5
- MySQL 入门:Case 语句很好用
- C#异步和多线程
- path development介绍
- js中的闭包
- MediaRecorder录制屏幕时在部分机型上报错prepare failed:-22
- Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
- "Obs" start pushing flow failure: the Output. The StartStreamFailed call process
- 【Harmony OS】【FAQ】鸿蒙问题合集1
猜你喜欢
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
【Harmony OS】【ARK UI】ETS 上下文基本操作
v-text指令:设置标签内容
工程制图-齿轮
数字化时代,企业如何建立自身的云平台与商业模式的选择?
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
移动流量的爆发式增长,社交电商如何选择商业模式
Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
那些让电子工程师崩溃瞬间,你经历了几个呢?
随机推荐
leetcode刷题学习之路
接口测试框架实战(四)| 搞定 Schema 断言
3.张量运算
DDL操作数据库、表、列
C# WPF设备监控软件(经典)-上篇
How many moments have you experienced the collapse of electronic engineers?
工程制图第九章作业
Jmeter 模拟多用户登录的两种方法
接口测试框架实战(三)| JSON 请求与响应断言
WinForm的控件二次开发
数字化时代,企业如何建立自身的云平台与商业模式的选择?
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
【翻译】开发与生产中的Kubernetes修复成本对比
Shell之条件语句
那些让电子工程师崩溃瞬间,你经历了几个呢?
easyswoole的mysqli 事务怎么写
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
MediaRecorder录制屏幕时在部分机型上报错prepare failed:-22
10.预测房价:回归问题
2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫