当前位置:网站首页>Garbage collection mechanism of browser
Garbage collection mechanism of browser
2022-06-26 20:03:00 【Cat's jingle】
JavaScript Have automatic garbage collection mechanism , When a value , When a reference is lost in memory , The garbage collection mechanism finds it according to a special algorithm , And recycle it , Free memory .
One 、 The concept of garbage collection
When some data is not needed , These data are garbage data , Garbage occupied memory , So it needs to be recycled and cleaned .
such as
let dog = new Object();
let dog.a = new Array(1);
When JavaScript When executing this code , You will first add a in the global scope dog attribute , And create an empty object in the heap , Point the address of the object to dog.
Then create another one with the size of 1 Array of , And point the attribute address to dog.a. The memory layout is shown below :
If at this time , I assigned another object to a attribute , The code is as follows :
dog.a = new Object()
Memory layout at this time :
a The direction of has changed , At this point, the array object in the heap becomes unused data , The technical term is inaccessible data . This is the garbage data that needs to be recycled .
Two 、 Garbage collection algorithm
First step : Reachable values in tag space :
From the root node (Root) set out , Traverse all objects .
Objects that can be traversed , It's reachable (reachable).
Objects that have not been traversed , Inaccessible (unreachable)
The second step : Recycling 「 Unreachable 」 The memory occupied by the value of
After all marking is completed , Clean up all unreachable objects in memory .
The third step , Memory consolidation
After frequent recycling of objects , There will be a lot of Discontinuous Space in memory , The technical term is memory fragmentation .
When a large number of memory fragments appear in the memory , If you need to allocate large contiguous memory , You may run out of memory .
So the last step is to defragment the memory
3、 ... and 、 Generational collection
Browsers divide data into two types , One is a temporary object , One is a permanent object . These two objects correspond to different recycling strategies , therefore ,V8 The heap is divided into two areas, the Cenozoic and the Paleozoic , Temporary objects are stored in the Cenozoic , Storing long-term objects in the old generation . And let the auxiliary garbage collector be responsible for the garbage collection of the new generation 、 The main garbage collector is responsible for the garbage collection of the old generation .
Temporary objects :
Most objects live in memory for a short time .
For example, variables declared inside functions , Or a variable in a block level scope . When the execution of a function or code block ends , The variables defined in the scope are destroyed .
Such objects quickly become inaccessible , It should be recycled quickly .
Secondary garbage collector
Responsible for the garbage collection of the new generation , Usually only support 1~8 M The capacity of .
Once the space is detected to be full , Just perform garbage collection .
Because the secondary garbage collector operates frequently , So in order to be efficient , In general, the space of the new area will be set to be smaller .
The Cenozoic is divided into two regions : Half the object area , Half of it is free space .
The newly added objects are put into the object area , When the object area is almost full , Will perform a garbage cleaning .
Mark all garbage in the object area first .
After marking , The surviving objects are copied to the free area , And arrange them in an orderly way .
The secondary garbage collector has no defragmentation . Because the free area is orderly at this time , There are no fragments , There's no need to sort it out .
After copying , The object area will be swapped with the free area . Put the living objects in the free area into the object area .
Complete garbage collection
Long term object :
Objects with a long life cycle , For example, the overall situation window、DOM、Web API wait .
Such objects can be recycled slowly .
Main garbage collector
Responsible for the garbage collection of the old generation , There are two characteristics :
Objects take up too much space .
Objects live long .
Use the tag ---- The clean algorithm performs garbage collection :
Mark
Start with a set of root elements , Recursively traverses this set of root elements .
In this traversal , Elements that can be reached are called active objects , Elements that do not arrive can be judged as garbage data .
eliminate
Directly clean up the data marked as garbage .
Memory consolidation
Mark more than once - After removal , A large number of discontinuous memory fragments will be generated , Memory defragmentation is required .
Four 、 Incremental collection
If there are many objects in the script , The engine traverses the entire object at once , Will cause a long pause
So the engine divides the garbage collection effort into smaller chunks
One part at a time , Multiple processing .
5、 ... and 、 Collect at leisure
The garbage collector will only be in CPU Try to run... When you are free , To reduce the possible impact on code execution
边栏推荐
- JSONUtils工具类(基于alibaba fastjson)
- 阿里云个人镜像仓库日常基本使用
- 黑客用机器学习发动攻击的九种方法
- Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
- Selection of database paradigm and main code
- IK分词器
- 项目实战五:搭建ELk日志收集系统
- Chain game development finished product source code chain game system development details
- Gd32 USB composite device file descriptor
- [recommended collection] these 8 common missing value filling skills must be mastered
猜你喜欢

(树) 树状数组

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

Record of user behavior log in SSO microservice Engineering

IK word breaker

Installation and use of filebeat

Minimum spanning tree, shortest path, topology sorting, critical path

Review of watermelon book (VII): Bayesian classifier (manual push + code demo)

关于不等式取值转义的思路

Good thing recommendation: mobile terminal development security tool

Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
随机推荐
抖音实战~首页视频~下拉刷新
Boot的单元测试
515. find the maximum value in each tree row
读书笔记:《过程咨询 III》
Good thing recommendation: mobile terminal development security tool
阿里云个人镜像仓库日常基本使用
Using cache in vuex to solve the problem of data loss in refreshing state
Analysis on development technology of NFT meta universe chain game system
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
MySQL - database creation and management
项目实战六:分布式事务-Seata
30. 串联所有单词的子串
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
Redis single sign on system + voting system
Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
Can I open an account online? Is it safe?
Flutter TextField详解
开户可以在网上开么?能安全吗?
Sword finger offer II 091 Paint the house
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布