当前位置:网站首页>Garbage collection mechanism
Garbage collection mechanism
2022-06-25 10:35:00 【m0_ forty-nine million four hundred and seventy-one thousand si】
The memory life cycle is basically consistent :
- Allocate the memory you need
- Use the allocated memory ( read 、 Write )
- Release it when it is not needed / The return

Reference resources :JS Memory management - Simple books
JS Summary of garbage collection and memory leaks in - Play chess at leisure - Blog Garden
Scoring point Stack garbage collection 、 Garbage collection 、 New area old area 、Scavenge Algorithm 、 Mark - Clear algorithm 、 Mark - Sorting algorithm 、 A complete pause 、 Increment mark Standard answer
The browser garbage collection mechanism is divided into stack garbage collection and heap garbage collection according to the storage mode of data .
Stack garbage collection is very simple , When a function is finished ,JavaScript The engine will move down ESP To destroy the execution context stored in the stack , Follow the principle of first in, then out .
Garbage collection , When the function ends directly , Stack space processing is complete , But the data in the heap space is not referenced , But it is still stored in heap space , The garbage collector is required to recycle the garbage data in the heap space . In order to make garbage collection achieve better results , Depending on the life cycle of the object , Use different garbage collection algorithms . stay V8 It will be divided into two parts: the new generation and the old generation , In the new generation, objects with short survival time are stored , A long-lived object stored in an old generation . Used in New Area Scavenge Algorithm , Use the mark in the aged area - Clear algorithms and tags - Sorting algorithm .
The bonus answer is
Scavenge Algorithm : 1. Mark : Mark the garbage in the object area 2. Clean up garbage data and defragment memory : The secondary garbage collector copies these surviving objects into the free area , And arranged in an orderly way , After copying, there will be no memory fragmentation in the free area 3. Role flip : Once the copy is done , Object area and free area for role flipping , That is, the original object area becomes a free area , The original free area becomes the object area , This completes the garbage object recycling operation , At the same time, this kind of role reversal operation can make these two areas in the new generation infinitely reused Mark - Clear algorithm : 1. Mark : The tagging phase starts 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 . 2. eliminate : Clean up the garbage data . 3. Generate memory fragmentation : Mark a block of memory multiple times - After clearing the algorithm , A large number of discontinuous memory fragments will be generated . Too much fragmentation will cause large objects to not be allocated enough contiguous memory . Mark - Sorting algorithm 1. Mark : And tags - The marking process is the same , Start with a set of root elements , Recursively traverses this set of root elements , In this traversal , Accessible elements are marked as active objects . 2. Arrangement : Let all live objects move to the end of memory 3. eliminate : Clean up memory outside the end boundary V8 The secondary garbage collector and the main garbage collector are used to deal with garbage collection , But due to the JavaScript It runs on the main thread , Once the garbage collection algorithm is executed , We need to JavaScript The script pauses , After garbage collection, resume script execution . We call this behavior total pause . In order to reduce the old generation of garbage recycling caused by the carton ,V8 The tagging process is divided into sub tagging processes , At the same time, let the garbage collection mark and JavaScript Apply logic alternately , Until the marking phase is complete , We call this algorithm incremental tagging (Incremental Marking) Algorithm
边栏推荐
- View. post VS Handler. Differences and usage scenarios of post
- How do wechat applets make their own programs? How to make small programs on wechat?
- How to make a self-service order wechat applet? How to do the wechat order applet? visual editing
- Basic usage and principle of schedulemaster distributed task scheduling center
- 国信证券证券账户开户安全吗
- 持续交付-Jenkinsfile 语法
- Solutions using protobuf in TS projects
- 输出式阅读法:把学到的知识用起来
- [200 opencv routines] 210 Are there so many holes in drawing a straight line?
- WPF Prism框架
猜你喜欢

How to install SSL certificates in Microsoft Exchange 2010

ES 学习

Opencv learning (II) -- installing opencv on raspberry pie

How to build a wechat applet? How to open an applet?

ShardingSphere-Proxy 4.1 分庫分錶

我希望按照我的思路尽可能将canvas基础讲明白

Deep understanding of JVM - JVM memory model

【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?

【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized

The real difference between i++ and ++i
随机推荐
【RPC】I/O模型——BIO、NIO、AIO及NIO的Rector模式
OODA working method
Flask blog practice - realize personal center and authority management
依赖属性、依赖附加属性以及类型转换
浅谈二叉树
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code
Flask博客实战 - 实现个人中心及权限管理
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
Floating window --- create an activity floating window (can be dragged)
Floating window --- create a system floating window (can be dragged)
Webapi performance optimization
WPF binding expression and binding data source (I)
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
网络协议学习---LLDP协议学习
Principle of distribution: understanding the gossip protocol
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
How much does a wechat applet cost? Wechat applet development and production costs? Come and have a look
New school: no fraud Economics
Byte interview: under what scenario will syn packets be discarded?
DigiCert和GlobalSign单域名OV SSL证书对比评测