Interviewer : Today is still coming to talk about the CMS garbage collector?
candidate : Hmm...
candidate : If you use the garbage collectors of the Seria and Parallel series: during garbage collection, the user thread will completely stop until the garbage collection is over!
candidate : Full name of CMS: Concurrent Mark Sweep, which translates as "Concurrent Mark Sweep"
candidate : Compare the above garbage collectors (Seria, Parallel and parNew) with CMS: its biggest difference is "concurrency": when the GC thread is working, the user thread "will not completely stop", the user thread is in "In some scenarios" execute concurrently with the GC thread.
candidate : However, it must be understood that no matter what garbage collector it is, Stop The World is definitely unavoidable!
candidate : CMS only allows GC threads to execute concurrently with user threads in "partial" GC scenarios
candidate : The design goal of CMS is to avoid "long-term" lag in the "old generation GC" (Stop The World)
Interviewer : Do you know the workflow of CMS?
candidate : Only understand a little bit, not much.
candidate : CMS can be simply divided into 5 steps: initial marking, concurrent marking, concurrent pre-cleaning, remarking and concurrent cleaning
candidate : It is not difficult to see from the steps that CMS mainly implements the "mark removal" garbage collection algorithm
Interviewer : Well... yes
candidate : I will start with the "initial mark"
Candidate : "Initial Marking" will mark objects "directly associated" with GCRoots and objects whose "young generation" points to "old generations"
candidate : "Initial marking" This process will happen Stop The World. But the speed at this stage is very fast, because there is no "tracking down" (only one level is marked)
candidate : After the "initial marking" is over, it enters the "concurrent marking" stage.
candidate : The process of "concurrent marking" does not stop the user thread (Stop The World does not occur). This stage is mainly to "track" downwards from GC Roots and mark all reachable objects.
candidate : "Concurrent marking" is more time-consuming from the perspective of GC (requires retrospective)
candidate : After the "concurrent marking" stage is completed, it is the "concurrent preprocessing" stage.
Candidate : "Concurrent preprocessing" This stage mainly wants to do: I hope to reduce the time consumed in the next stage of "relabeling"
candidate : Because the next stage of "re-marking" requires Stop The World
Interviewer : Well...
Candidate : "Concurrent marking" At this stage, because the user thread is not suspended, the object may change
candidate : There may be some objects that have been promoted from the young generation to the old generation. Some objects may be directly allocated to the old generation (large objects). Maybe the object references of the old or young generation have changed...
Interviewer : How to solve this problem?
Candidate : For objects in the old age, in fact, you can still use card table storage (mark the card page corresponding to the change in the old age object as dirty)
candidate : So the "concurrent preprocessing" stage will scan objects that may have changed in the old age due to "concurrent marking", and will scan the card page marked as dirty again
Interviewer : Well...
candidate : For objects in the new generation, we still have to traverse the new generation to see if there are any objects that refer to the old generation during the "concurrent marking" process..
candidate : However, the JVM provides us with a lot of "parameters", and it is possible that a minor GC will be triggered during this process (triggering a minor GC means that you can traverse the new generation of objects less)
candidate : After the "concurrent preprocessing" phase is over, it comes to the "re-marking" phase
candidate : The "re-marking" phase will stop the world. The pause time of this process is actually largely determined by the above "concurrent preprocessing" phase (it can be found that this is a catch-up process: while marking surviving objects , While the user thread generates garbage during execution)
candidate : The last is the "concurrent cleanup" phase, and will not Stop The World
candidate : While the user thread is executing, the GC thread is recycling unreachable objects
candidate : In this process, it is still possible that user threads continue to generate garbage, but it can only be left to the next GC for processing. The generated garbage is called "floating garbage"
candidate : After finishing, the internal data related to the CMS algorithm will be reset to prepare for the next GC cycle
Interviewer : Well, I understand the recycling process of CMS
Interviewer : listens to it, in fact, the garbage collection process is "subdivided", and then at certain stages, the user thread can not be stopped, and garbage is collected while processing the request, so as to reduce the stop of each garbage collection. The time of The World
Interviewer : Of course, many optimizations have been made in the middle (dirty card mark, minor gc may be triggered midway, etc., in my understanding, these should all provide CMS related parameter configuration)
Interviewer : However, I think that many companies are now using G1, so what do you think are the shortcomings of CMS?
candidate : 1. Space needs to be reserved: The CMS garbage collector can collect garbage while processing user threads. It is necessary to ensure that there is sufficient memory space for users in this process.
candidate : If the reserved space during CMS operation is not enough, an error (Concurrent Mode Failure) will be reported, and the Serial Old garbage collector will be started for garbage collection in the old generation, which will cause a long pause.
candidate : Obviously, how much space is reserved, there must be parameter configuration
candidate : 2. Memory fragmentation problem: CMS is essentially a collector that implements the "mark removal algorithm" (you can see from the process), which means that memory fragmentation will occur
candidate : due to too much fragmentation, it may trigger full GC due to insufficient memory space. CMS generally defragments the fragmentation in the process of triggering full GC
Candidate : Sorting involves "moving"/"marking", then this process will definitely Stop The World. If the memory is large enough (meaning that there are enough objects that may be loaded), then this process requires a certain amount of lag Time.
Interviewer : Well...
candidate : The disadvantage of using CMS seems to be an endless loop:
candidate : 1. Excessive memory fragmentation, resulting in reduced space utilization.
candidate : 2. The space itself needs to be reserved for user threads. Now the fragmented memory has exacerbated the space problem, leading to the possibility that the garbage collector will be downgraded to Serial Old and the stall time will be longer.
candidate : 3. To deal with the problem of memory fragmentation (organization), it will also be stuck
candidate : However, technical implementation is a trade-off, it is impossible for you to do everything perfectly
candidate : It is very interesting to understand this process
Interviewer : Do you know the G1 garbage collector?
candidate : Only understand a little bit, not more
Candidate : But, save it until next time, let you digest it first, or I'm afraid you can't stand it anymore.
This article summarizes :
- CMS Garbage Collector Design Purpose : In order to avoid "long-term" lag in the "old generation GC" (Stop The World)
- CMS garbage collector recycling process : initial marking, concurrent marking, concurrent preprocessing, remarking and concurrent removal. The two stages of initial marking and remarking will stop the world
- CMS garbage collector : Memory fragmentation && need space reservation: the pause time is unpredictable
Welcome to follow my WeChat public [1618c6b1fa1892 Java3y ] to talk about Java interviews, the online interviewer series is being updated continuously!
[Online Interviewer-Mobile] The series updated twice a week!
[Online Interviewer-Computer] The series updated twice a week!
Originality is not easy! ! Seek three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。