Steady! I prepared a CMS garbage collector for one night

Java3y 2021-11-25 17:40:53

interviewer Let's talk today CMS Garbage collector ?

candidates : Yeah …

candidates : If you use Seria and Parallel A series of garbage collectors : During garbage collection , All user threads stop completely , Until the garbage collection is over !

candidates :CMS Full name :Concurrent Mark Sweep, Which translates to 「 Concurrent tag removal 」

candidates : use CMS Compare the garbage collector above (Seria and Parallel and parNew): Its biggest difference is 「 Concurrent 」: stay GC When threads work , User threads 「 It won't stop completely 」, User thread in 「 In some scenes 」 And GC Threads execute concurrently .

candidates : however , To understand is , No matter what the garbage collector is ,Stop The World It must be inevitable !

candidates :CMS It's just 「 part 」 Of GC You can make GC Threads execute concurrently with user threads

candidates :CMS Our design goal is to avoid 「 Old age GC」 appear 「 Long-term 」 Carlton of (Stop The World)

interviewer Then you know CMS Your workflow ?

candidates : Only know a little , No more .

candidates :CMS It can be simply divided into 5 A step : Initial marker 、 Concurrent Tags 、 Concurrent pre cleanup 、 Retag and concurrent cleanup

candidates : It is not difficult to see from the steps ,CMS Mainly achieved 「 Mark clear 」 Garbage collection algorithm

interviewer : Um. … Yes

candidates : I'm from 「 Initial marker 」 Let's get started

candidates :「 Initial marker 」 Will mark GCRoots「 Directly related 」 And 「 The younger generation 」 Point to 「 Old age 」 The object of

candidates :「 Initial marker 」 This process will happen Stop The World Of . But the speed of this stage is very fast , Because no 「 Back down 」( Mark only one layer )

candidates : stay 「 Initial marker 」 After that , It's in 「 Concurrent Tags 」 Stage

candidates :「 Concurrent Tags 」 This process does not stop the user thread ( Not going to happen Stop The World). This stage is mainly from GC Roots Down 「 trace 」, Mark all reachable objects .

candidates :「 Concurrent Tags 」 stay GC From the point of view , It's more time consuming ( Need to trace )

candidates :「 Concurrent Tags 」 After this stage is finished , That's it 「 Concurrent preprocessing 」 Stage

candidates :「 Concurrent preprocessing 」 What I want to do at this stage : Hope to reduce the next stage 「 Re label 」 The time it takes

candidates : Because the next stage 「 Re label 」 Is the need to Stop The World Of

interviewer : Um. …

candidates :「 Concurrent Tags 」 At this stage, because the user thread is not suspended , So the object is likely to change

candidates : There may be some objects , From the new generation to the old age . There may be some objects , Directly allocated to the old age ( Big object ). Maybe the object references of the old or new generation have changed …

interviewer So this question , How to solve it ?

candidates : For the elderly , In fact, you can still use classes card table The storage ( Mark the card page corresponding to the change of the old age object as dirty)

candidates : therefore 「 Concurrent preprocessing 」 This phase will be scanned, possibly due to 「 Concurrent Tags 」 The objects that cause changes in the elderly generation , It will be scanned again and marked as dirty Card page

interviewer : Um. …

candidates : For the new generation of objects , We still have to traverse the Cenozoic to see where 「 Concurrent Tags 」 Is there any object in the process that references the old age ..

candidates : however JVM It provides us with a lot of 「 Parameters 」, It is possible to trigger once in this process minor GC( Triggered minor GC It means less traversal of the new generation of objects )

candidates :「 Concurrent preprocessing 」 At the end of this phase , That's it 「 Re label 」 Stage

candidates :「 Re label 」 Stage meeting Stop The World, The pause time of this process actually depends largely on the above 「 Concurrent preprocessing 」 Stage ( You can find , This is a catch-up process : While marking the living object , While the user thread is executing, it generates garbage )

candidates : The last is 「 Concurrent elimination 」 Stage , Can't Stop The World

candidates : While the user thread is executing , On one side GC The thread is reclaiming unreachable objects

candidates : This process , It is still possible that user threads are constantly generating garbage , But only for the next time GC processed , The waste produced is called “ Floating garbage ”

candidates : It will reset when it is finished CMS Algorithm related internal data , For the next time GC Prepare for the cycle

interviewer : Um. ,CMS The recycling process , I know the

interviewer Listen , In fact, it is the process of garbage collection to ” To subdivide ” 了 , Then, at some stage, the user thread can not be stopped , While recycling garbage , While processing the request , To reduce each garbage collection Stop The World Time for

interviewer : Of course! , A lot of optimization has also been done in the middle (dirty card Mark 、 May trigger... Halfway minor gc wait , In my understanding , These should all provide CMS Related parameter configuration of )

interviewer however , I think many enterprises are using G1 了 , Then you think CMS What are the disadvantages ?

candidates :1. Space needs to be reserved :CMS The garbage collector can collect garbage at the same time , While processing user threads , It is necessary to ensure that there is sufficient memory space for users in this process .

candidates : If CMS There is not enough space reserved during operation , Will report a mistake (Concurrent Mode Failure), This will start Serial Old The garbage collector carries out garbage collection in the old days , It will cause a long pause .

candidates : Obviously , How much space is reserved , There must be parameter configuration

candidates :2. Memory fragmentation problem :CMS In essence, it realizes 「 Tag clearing algorithm 」 The collector of ( From the process, we can see ), This would mean memory fragmentation

candidates : Because there are too many pieces , It may also be triggered by insufficient memory space full GC,CMS It usually triggers full GC This process defragments

candidates : Sorting involves 「 Move 」/「 Mark 」, Then this process will Stop The World Of , If memory is large enough ( It means that there may be enough objects loaded ), Then this process will take some time .

interviewer : Um. …

candidates : Use CMS The disadvantage of is like a dead circle :

candidates :1. Too many memory fragments , Resulting in reduced space utilization .

candidates :2. Space itself needs to be reserved for user threads , Now fragmented memory exacerbates the problem of space , This may cause the garbage collector to be downgraded to Serial Old, Caton takes longer .

candidates :3. To deal with memory fragmentation ( Arrangement ), It will also get stuck

candidates : however , Technology implementation is a kind of trade-off( Balance ), You can't do everything perfectly

candidates : Understand the process , It's very interesting

interviewer : that G1 Garbage collector, do you know

candidates : Only know a little , No more

candidates : however , Save it for next time , Let you digest , Otherwise I'm afraid you can't hold it .

This paper summarizes

  • CMS The garbage collector is designed for : for fear of 「 Old age GC」 appear 「 Long-term 」 Carlton of (Stop The World)
  • CMS Garbage collector recycling process : Initial marker 、 Concurrent Tags 、 Concurrent preprocessing 、 Retag and concurrent cleanup . Initial marking and re marking will Stop The World
  • CMS The disadvantages of garbage collector : Memory fragmentation will occur && Space reservation is required : The pause time is unpredictable

Welcome to my WeChat official account. 【Java3y】 To have a chat Java interview , The online interviewer series is continuously updated !

【 On line interviewers - Mobile 】 series Two continuous updates a week !
【 On line interviewers - Computer terminal 】 series Two continuous updates a week !

Originality is not easy. !! Ask for three company !!

Please bring the original link to reprint ,thank
Similar articles