当前位置:网站首页>Talk about GC mechanism often asked in interview
Talk about GC mechanism often asked in interview
2022-06-24 22:47:00 【mb61de96e0d1987】
GC Garbage collection , It is a mechanism to reclaim memory space and avoid memory leakage . When JVM Memory is tight , Through execution GC Effectively reclaim memory , Instead, it is allocated to new objects for memory reuse . JVM GC Although the mechanism does not need to develop active participation , Reduce a lot of work , But in some cases , Automatically GC System performance will be degraded , Slow response , So we need to know and master in advance GC Mechanism . When faced with this situation , In order to solve the problem calmly .
in addition GC The mechanism is also Java Interview high frequency questions , Understand and master GC It's a necessary skill .
Study GC , First, we solve three problems :
- What is rubbish
- Where to recycle garbage
- How to recycle garbage
What is rubbish #
Let's start with a simple piece of code .

The above code converts a string object into a byte array , Then write the local file . Method once started , It will allocate a certain amount of memory to the new object , Then I tell you the quotation str , bytes Variable . Wait until the method is finished , The method's internal local variables are then destroyed . But this just destroys the local variables , Without taking away the actual objects in memory . It doesn't work anymore , There are no referenced objects , Classify it as rubbish .
There are so many objects in the huge memory , GC You need to mark these objects accurately before , It can be divided into living objects and garbage objects . Once the process is less marked , Then we have to wait for the next time GC Mark , Recycle , This will affect GC efficiency . In addition, we must not mislabel , Mark healthy living objects as garbage . Once the normal living objects are recovered , May cause all kinds of program crashes .
There are currently two algorithms that can be used to mark :
- Reference counting
- Accessibility analysis
Reference counting #
Reference counting by assigning a field to the object header , Used to store the object reference count . Once the object is referenced by another object , Count plus 1. If this reference fails , Count minus 1. When the quoted count is 0 when , Represents that the object is no longer referenced , Can be recycled .

Reference counting
As shown in the figure above , When str When referencing objects in the heap , The count is increased to 1. When str Turn into null when , No more references to the object , Subtract the count 1. At this point, the object can be GC Recycling .
The counting method only needs to judge the count value , So the implementation is relatively simple , This process is also more efficient . But there is a serious problem , Can't solve object circular reference problem .

Reference counting -1
You can see from the above picture that , a , b No more references to objects in the heap , Causes the count to decrease by one . At this time, there are also mutual references within the two objects , The count value is not 0, here GC There's no way to recycle the object .
Accessibility analysis #
This algorithm first needs to find the active references according to the rules , Call it GC Roots . And then GC Roots Start as the root node , Traversal object reference diagram , Will be traversable ( Can be up to ) Object marked as alive , The rest of the objects are useless .

Accessibility analysis
Notice this is quote , Not the object .
You can see from the above picture that , Although there are circular references to green objects , But because these objects can't be GC Roots Traversing , So it will be recycled .
Can be treated as GC Roots Active references include, but are not limited to, the following :
- Method
- Static variables , Constant
- JNI handles
- ....
Where to recycle garbage #
Remember just beginning to touch Java when , Just the stack , Object instances are allocated in the heap , The local variable in the method is in the stack . actually JVM Memory area division is more detailed , It is divided into :
- Pile up
- Method area
- Virtual machine stack
- Native Method Stack
- Program counter

JVM Runtime memory partitioning
As shown in the figure , We divide memory into thread private and thread shared areas . Method area and heap are both thread shared areas , These two parts occupy JVM Most of the memory , The remaining three kids will be bound to the thread , As threads die , Automatically will be JVM Recycling .
Pile up
The heap should be the most familiar area , Almost all object instances will be born here , It is also the largest area of memory occupied by virtual machine , It is JVM Cell phone in memory . The inner part of heap memory is not just a piece , At present, it will be based on the generational algorithm , Divide the pile into generations , Different objects are in different areas . We will learn more about this later .
Method area
The method area will save the class information that has been loaded on the virtual 、 Constant , Static variables , Byte code and other information , Objects on the heap formally pass this information through the method area , In order to create it correctly .
Stack
The virtual machine stack consists of a series of stack frames , Each stack frame actually represents a method , A local variable table of the method will be saved in the stack frame , Method access information , Operation stack, etc . Every time a method is called , It will push the stack frame into the stack , end of execution , Out of the stack .
The local method stack is similar to the virtual machine stack , The biggest difference is , The virtual machine stack performs Java Method , And the local method stack will be used to execute Native Method service . The following methods will be executed in the local method stack .
<pre class="java" style="margin: 10px 0px; padding: 0px; white-space: pre !important; overflow-wrap: break-word; position: relative !important; color: rgb(49, 70, 89); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
Copy
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); </pre>
Program counter
The program calculator can be said to be the smallest part of these areas , But the function is very important .Java Source code is compiled into bytecode , Then be JVM After loading and running , It's going to be one command at a time , And the job of the program counter is to tell the current thread that the next instruction needs to be executed . So even if thread switching happens , Waiting for recovery , The current thread still knows the instructions to be executed next .
How to recycle
At present the mainstream GC There are three kinds of algorithms :
- Mark - Clear algorithm
- Copy algorithm
- Mark - Sorting algorithm
Mark - Clear algorithm
This is the most basic and easy to implement algorithm , The main implementation steps are divided into two steps : Mark , eliminate .
- Mark : Through the above GC Roots Mark the reachable object .
- eliminate : clear Unmarked object .

ps: This picture is really difficult to draw ....
You can see that after the algorithm is recycled , Although the heap space was cleared , But it also produces a lot of space debris . This causes a new object to calculate based on the remaining heap capacity , Looks like it can be allocated , But the actual distribution process , Because there is no continuous memory , Causes the virtual machine to sense a lack of memory , I have to trigger it again in advance GC .
Maybe you'll have doubts here , Why do objects need to allocate a contiguous block of memory ?
Here's a quote R god @RednaxelaFX answer .

In addition, this algorithm has another disadvantage : The efficiency of marking and removal is relatively low . This should lead to GC It takes too long , Affect normal program use .
Copy algorithm
In order to solve the above efficiency problems , Birth of replication algorithm . This algorithm divides the available memory into two parts , Use only one piece at a time , When this block of memory is used , Trigger GC , The surviving objects will be copied to another piece in turn , Then clean up the used memory at one time .


This algorithm only needs to operate half of the memory at a time , GC After recycling, there is no space debris , New object memory allocation only needs to move the heap top pointer , Allocate memory in order , Implement a simple , Efficient operation . But this algorithm leaves half the memory space , Space utilization efficiency is not high .
PS: Copy algorithm in space for time , You can't have both
In addition, the object survival rate will also affect the efficiency of the replication algorithm . If most of the objects are dying day by day , Just move a few surviving objects , You can make most of the space . On the contrary, if the survival rate of the object is high , This requires more copying operations , After recycling, there is no extra memory , This can lead to frequent triggers GC .
For this long-lived object , You need to use markers - Sorting algorithm .
Mark - Sorting algorithm
Mark - Sorting algorithm can be said to be mark - An improved version of the cleanup algorithm , Improved removal of space debris . This algorithm is divided into two steps :
GC Roots
- 1.


Although the mark - The sorting algorithm solves the problem of marking - The problem of clearing algorithm space debris , Also make full use of the whole memory space , But this algorithm is not efficient . Compared to the mark - Clear algorithm , Mark - More sorting algorithms add this step , So the efficiency of the algorithm is still lower than that of the mark - Clear algorithm .
Generational collection algorithm
From the top three GC The algorithm can see , There is no perfect algorithm for space and time efficiency , So what we can do is to make use of all kinds of algorithm features and apply them to the unused memory area .
At present, the commercial virtual machine divides the memory area according to the life cycle of the object , Generally divided into the new generation , Old age . In general, new objects will be given priority in the new generation , If the survival time of new generation objects is greater than a certain threshold , Will move to the old age . The targets of the new generation are all short-lived ghosts , The object of the old age is Mr. longevity .
Every time the new generation GC After that, a large number of objects can be recycled , So it's more suitable for replication algorithm , It costs only a small amount to copy the surviving objects . Here the memory is not divided according to 1:1 Divide , The default will be as follows 8:1:1 Divide into Eden And two Survivor Space . Each use Eden Together with Survivor Space , So we're just idle 10% Memory space . But every time we recycle, we can't guarantee that the surviving objects are smaller than 10%, In this case, we need to rely on the memory allocation guarantee of the old age . When Survivor Space does not hold the remaining live objects , Move these objects to the old age through the distribution guarantee system .
The survival rate of the elderly will be particularly high , And there is no additional space for distribution guarantees , So it's not suitable for replication algorithms , So you need to use markers - To remove or mark - Sorting algorithm .
Last
For everyone Share An article compiled by front-line development Daniel java High concurrency core programming fairy document , The main knowledge points contained in it are : Multithreading 、 Thread pool 、 Built in lock 、JMM、CAS、JUC、 High concurrency design patterns 、Java Asynchronous callback 、CompletableFuture Class etc. .
It's not easy to code words , If you think this article is useful to you , Please give me one button three times ! Focus on author , There will be more dry goods in the future Share , Stay tuned !
边栏推荐
- Visitor tweets tell you which groups are consuming blind boxes
- NIO、BIO、AIO
- Problem solving - nested lists
- LeetCode Algorithm 剑指 Offer 52. 两个链表的第一个公共节点
- 倍加福(P+F)R2000修改雷达IP
- Future development of education industry of e-commerce Express
- 结构体的内存对齐
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- The difference between get and post
- Concurrency of heap memory allocation
猜你喜欢

String exercise summary 2

Combine pod identity in aks and secret in CSI driver mount key vault

ACL (access control list) basic chapter - Super interesting learning network
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020

Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years

Redis-跳表

nuScenes——数据集配置过程中遇到图像文件缺失或大小为0时的补救方法

详细了解Redis的八种数据类型及应用场景分析

How to automatically remove all . orig files in Mercurial working tree?

糖豆人登录报错解决方案
随机推荐
FANUC机器人_KAREL编程入门学习(1)
网上立案流程
Chapter 10 project communication management
NIO多路复用之Selector的使用
Fanuc robot_ Introduction to Karel programming (1)
String exercise summary 2
[ingénierie logicielle] points clés à la fin de la période
Certificate photo processing
面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
ACL (access control list) basic chapter - Super interesting learning network
Combine pod identity in aks and secret in CSI driver mount key vault
Redis hop table
img2pdf
源码阅读 | OpenMesh读取文本格式stl的过程
大厂面试必问:如何解决TCP可靠传输问题?8张图带你详细学习
img2pdf
LeetCode Algorithm 剑指 Offer II 027. 回文链表
Virtual private network foundation
[Software Engineering] key points at the end of the period
Principles of Ethernet port mirroring, link aggregation and VLAN Technology