当前位置:网站首页>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
边栏推荐
- Webapi performance optimization
- A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
- Google Earth engine (GEE) - evaluate enables one click batch download of all single images in the research area (some areas in Shanghai)
- Es learning
- 我的作文题目是——《我的区长父亲》
- 垃圾回收机制
- 无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
- Principle of distribution: understanding the gossip protocol
- Is it safe to speculate in stocks by mobile phone?
- Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
猜你喜欢

Flask blog practice - realize the latest articles and search in the sidebar

ShardingSphere-Proxy 4.1 分庫分錶

This is enough for request & response

XSS攻击

Redis (II) distributed locks and redis cluster construction

网络协议学习---LLDP协议学习

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

How much does a small program cost? How much does a small program cost? It's clear at a glance

Linked list delete nodes in the linked list

On binary tree
随机推荐
虚幻引擎图文笔记:使用VAT(Vertex Aniamtion Texture)制作破碎特效(Houdini,UE4/UE5)上 Houdini端
DDS learning notes of opendds
单片机进阶---PCB开发之照葫芦画瓢(二)
How to make a self-service order wechat applet? How to do the wechat order applet? visual editing
This is enough for request & response
Shardingsphere proxy 5.0 sub database and sub table (I)
网络协议学习---LLDP协议学习
Webapi performance optimization
NetCore性能排查
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
On binary tree
Basic use and cluster construction of consult
WPF Prism框架
Shardingsphere proxy 4.1 sub database and sub table
Opencv learning (II) -- installing opencv on raspberry pie
一文了解Prometheus
Free applet making tool, how to make wechat applet
OpenCV学习(一)---环境搭建
手机炒股安全吗?
【论文阅读|深读】DRNE:Deep Recursive Network Embedding with Regular Equivalence