当前位置:网站首页>Garbage collection mechanism
Garbage collection mechanism
2022-06-24 03:49:00 【HZFEStudio】
Complete high frequency question bank warehouse address :https://github.com/hzfe/awesome-interview
Complete high frequency question bank reading address :https://febook.hzfe.org/
Related issues
- What is memory leak
- Common garbage collection algorithms
- How to troubleshoot memory leaks
Answer key points
Reference counting Mark removal method Mark-Compact( Tag to sort out )Scavenger( The sweeper )
GC(Garbage Collection, Garbage collection ) Is an automatic memory management mechanism , Garbage collector (Garbage Collector) It can automatically reclaim the memory allocated to the program that is no longer used . common GC Algorithms include reference counting method and mark clearing method .V8(JavaScript engine , Provide execution JavaScript The runtime environment for ) The garbage collector algorithm is mainly composed of Mark-Compact and Scavenger constitute .
In depth knowledge
1. Memory leak
Memory leak means , Objects that should be recycled are not recycled normally , Become a permanent object of the older generation , Resulting in higher and higher memory consumption . Memory leaks can cause applications to slow down 、 High delay 、 Breakdown and so on .
1.1 Memory life cycle
- Distribute : Allocate memory on demand .
- Use : Read and write allocated memory .
- Release : Free up memory you no longer need .
1.2 Common causes of memory leaks
- Create global variables , And there is no manual recycling .
- Event listener / Timer / Closures, etc. are not cleaned properly .
- Use JavaScript Object to cache , The expiration policy and object size control are not set .
- The problem of untimely consumption caused by queue congestion .
2. Reference Counting( Reference count )
Reference Counting Is a common garbage collection algorithm , The core idea is : Put resources ( Example object ) The number of citations of is saved , Release when the number of references is zero . The limitations of this method : When there is a Circular reference when , Objects that reference each other are not recycled .
3. V8 Garbage collection mechanism
V8 There are two garbage collectors in the . The main GC Use Mark-Compact Garbage collection algorithm , Collect garbage from the whole heap . small GC Use Scavenger Garbage collection algorithm , Collect new generation garbage .
Two different algorithms deal with different scenarios :
- Use Scavenger The algorithm mainly deals with Short life cycle Accessible objects in the object of .
- Use Mark-Compact The algorithm mainly deals with Long life cycle Inaccessible objects in the object .
Because there are a few accessible objects surviving in the Cenozoic , There are a few inaccessible objects in the older generation , Therefore, the combination of these two recycling algorithms is very efficient .
3.1 Generational garbage collection
stay V8 in , be-all JavaScript All objects pass Pile up To allocate .V8 Divide the heap it manages into two generations : The new generation and the old generation . The Cenozoic can be subdivided into two sub generations (Nursery、Intermediate).
That is, the objects in the Cenozoic are those with short survival time , Objects in an old generation are objects that have a long life or are memory resident .
3.2 Mark-Compact Algorithm (Major GC)
Mark-Compact The algorithm can be seen as Mark-Sweep( Mark clear ) Algorithm and Cheney A combination of replication algorithms . The algorithm is mainly divided into three stages : Mark 、 eliminate 、 Arrangement .
- Mark (Mark) Tags are the process of finding all accessible objects .GC From a set of known object pointers ( Called root set , Including execution stack and global objects ) in , Recursively mark accessible objects .
- eliminate (Sweep) Clearing is the memory space left by inaccessible objects , Add to free linked list (free list) The process of . When allocating memory for new objects in the future , You can reallocate from the free linked list .
- Arrangement (Compact) Collation is to put accessible objects , The process of moving to one end of memory . It mainly solves the problem after the mark removal stage , When there are many memory fragments in the memory space , May result in failure to allocate large objects , The problem of triggering garbage collection in advance .
3.3 Scavenger Algorithm (Minor GC)
V8 The new generation memory space is Scavenger Algorithm , The algorithm uses semi-space( Half space ) The design of the : Divide the pile into two , Always use only half the space :From-Space To use space ,To-Space For free space .
The new generation is in From-Space Objects assigned in ; In the garbage collection phase , Check and copy as needed From-Space The accessible objects in the are to To-Space Or the old generation , And release From-Space Memory space occupied by inaccessible objects in ; Last From-Space and To-Space Role exchange .
Reference material
边栏推荐
- SQL注入绕过安全狗思路一
- Use lightweight application server to automatically download and upload to onedrive
- How do websites use CDN? What are the benefits of using it?
- 在pycharm中pytorch的安装
- There is such a shortcut to learn a programming language systematically
- How to select the application of the server?
- Summary of common problems of real-time audio and video TRTC - quality
- Idea 1 of SQL injection bypassing the security dog
- LeetCode 2006. Number of pairs whose absolute value of difference is k
- How to build glasses website what are the functions of glasses website construction
猜你喜欢

【代码随想录-动态规划】T392.判断子序列
![[Numpy] Numpy对于NaN值的判断](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[Numpy] Numpy对于NaN值的判断

浅谈游戏安全 (一)

JVM调优简要思想及简单案例-怎么调优
![[numpy] numpy's judgment on Nan value](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[numpy] numpy's judgment on Nan value

老弹出explorer.exe遇到问题已停止工作,怎么办?

On game safety (I)

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

Modstartcms enterprise content site building system (supporting laravel9) v4.2.0
Thank you for your recognition! One thank-you note after another
随机推荐
Implement the throttling de dithering function
RPM 包的构建 - SPEC 基础知识
Record the creation process of a joke widget (I)
No monitoring information seen in kibana
Use lightweight application server to automatically download and upload to onedrive
[see you] on October 24, we met at Tencent Binhai building
LeetCode 1281. Difference of sum of bit product of integer
How to choose excellent server hosting or server leasing in Beijing
There is such a shortcut to learn a programming language systematically
Record a programming contest
Grpc: how to make grpc provide restful API services?
Pine Script脚本常用内容
Black hat actual combat SEO: never be found hijacking
Ar 3D map technology
黑帽SEO实战之目录轮链批量生成百万页面
Why do I need a code signing certificate? Where can I get a code signing certificate?
High quality travel on national day, visual start of smart Tourism
Received status code 502 from server: Bad Gateway
Use the fluxbox desktop as your window manager
The medical technology giant was blackmailed and Microsoft announced 74 security vulnerabilities | global network security hotspot