当前位置:网站首页>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边栏推荐
猜你喜欢

mysql 创建索引的三种方式

社交电商:流量红利已尽,裂变营销是最低成本的获客之道

Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes

接口测试实战| GET/POST 请求区别详解

链动2+1模式简单,奖励结构丰厚,自主裂变?

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

6.神经网络剖析

我将GuiLite移植到了STM32F4开发板上

超好用的画图工具推荐

Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
随机推荐
How many moments have you experienced the collapse of electronic engineers?
Browser listens for tab closing
DC-4靶场搭建及渗透实战详细过程(DC靶场系列)
I ported GuiLite to STM32F4 board
OpenFOAM extracts equivalency and calculates area
v-text指令:设置标签内容
正则表达式与绕过案例
redis键值出现 xacxedx00x05tx00&的解决方法
path development介绍
3.张量运算
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
12.机器学习基础:评估机器学习模型
MySQL 删除表数据,重置自增 id 为 0 的两个方式
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
2022 Henan Mengxin League Game (4): Zhengzhou University of Light Industry E - Sleep Well
2.何为张量
Redis连接不上的报错解决方案汇总
表的创建、修改与删除