当前位置:网站首页>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
边栏推荐
- Summary of common SSH commands
- Grpc: how to make grpc provide restful API services?
- Industrial security experts talk about how to build security protection capability for government big data platform?
- How do websites use CDN? What are the benefits of using it?
- A problem of testing security group access in windows10
- What is the difference between server leasing and hosting?
- 系统的去学习一门编程语言,原来有如此捷径
- golang clean a slice
- An open source monitoring data collector that can monitor everything
- How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
猜你喜欢

Black hat SEO actual combat search engine snapshot hijacking

ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0

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

ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计

Installation of pytorch in pycharm

Black hat actual combat SEO: never be found hijacking

On game safety (I)

JVM调优简要思想及简单案例-怎么调优

Koom of memory leak

Old popup explorer Exe has stopped working due to problems. What should I do?
随机推荐
How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
Disk partition extension using graphical interface and PowerShell code
黑帽实战SEO之永不被发现的劫持
Koom of memory leak
NLP task summary introduction and understanding
左滑从小窗到大窗口DispatchFrameLayout
内存泄漏之KOOM
What is the difference between server leasing and hosting?
Web penetration test - 5. Brute force cracking vulnerability - (4) telnet password cracking
Mocktio usage (Part 2)
getLocationInWindow源码
Prometheus PushGateway 碎碎念
Ar 3D map technology
Do you understand TLS protocol?
Record the creation process of a joke widget (I)
Troubleshooting and resolution of errors in easycvr calling batch deletion interface
3D visualization of Metro makes everything under control
Coding Ci of Devops
内存泄漏之KOOM-Shark中的Hprof信息
ClickHouse Buffer