当前位置:网站首页>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
边栏推荐
- 无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
- Performance file system
- Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
- The real difference between i++ and ++i
- How to do the wechat selling applet? How to apply for applets
- A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
- I hope to explain the basics of canvas as clearly as possible according to my ideas
- Difference between malloc and calloc
- Detailed explanation of Android interview notes handler
- 【论文阅读|深读】LINE: Large-scale Information Network Embedding
猜你喜欢
WPF Prism框架
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code
MCU development -- face recognition application based on esp32-cam
Shardingsphere proxy 4.1 sub database and sub table
What is CRA
Create menu file
Flask博客实战 - 实现侧边栏最新文章及搜索
Flask博客实战 - 实现侧边栏文章归档及标签
垃圾回收机制
[dynamic planning] - Digital triangle
随机推荐
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
How to build a wechat applet? How to open an applet?
Linked list delete nodes in the linked list
Flask blog practice - archiving and labeling of sidebar articles
New school: no fraud Economics
手机炒股安全吗?
Mqtt beginner level chapter
NetCore性能排查
戴尔科技演绎“快”字诀,玩转CI/CD
[paper reading | depth] role based network embedding via structural features reconstruction with degree regulated
国信证券证券账户开户安全吗
Flask博客实战 - 实现侧边栏最新文章及搜索
QT: parsing JSON
This is enough for request & response
Google Earth Engine (Gee) - evaluate réalise le téléchargement en un clic de toutes les images individuelles dans la zone d'étude (certaines parties de Shanghai)
手机办理广州证券开户靠谱安全吗?
宏的运用接续符\
Shardingsphere proxy 5.0 sub database and sub table (I)
Use of Siemens plcs7-200 (I) -- Introduction to development environment and configuration software
Difference between malloc and calloc