CMS Past and Present

CMS has always been a common test site in interviews. Today we will briefly introduce it in easy-to-understand language.


Why is the garbage collector partitioned?


image.png

As shown in the figure above: JVM virtual machine divides the heap memory area into generations. The old generation is the area where people live and die. The old age is the area where the old age does not die. Different age objects have different characteristics, so different garbage collectors are needed to process them. As shown in the figure below, the area to the left of the black vertical line is the generational garbage collector, and the memory is not generational after G1.


image.png


Single-threaded garbage collector: Serial + Serial Old


Serial (SY) and Serial Old (SO) are a combination of single-threaded garbage collectors, and the garbage collection thread is single-threaded. With the increasing size of modern memory areas, the combination of SY+SO has become less and less. The single thread of garbage collection will undoubtedly take longer STW time. This combination is more suitable for earlier JDK versions. As shown in the figure below, the user thread represents the application processing process, and the garbage collection thread represents the garbage thread cleaning up the garbage process. At this stage, the application needs to wait for the garbage thread STW.

image.png

Multi-threaded garbage collector: PS+PO


As we said earlier, the disadvantage of a single-threaded garbage collector is that when the memory area becomes larger, the collection efficiency will be very low, then OK, it will change, as shown in the figure below, a multi-threaded garbage processor.

image.png

It is worth noting that the PS+PO combination is JDK1.7, and JDK1.8 is the default garbage collector. You can view it on the Dos interface through the java -XX:+PrintCommandLineFlags command. As shown in the figure below, this command can view the default parameters of JVM initialization. For example: -XX:InitialHeapSize represents the initial heap size.

image.png


Why pop out a CMS+ParNew


Parallel processing is there, what does CMS+ParNew do? In fact, PO is concerned with throughput, while CMS is concerned with shortening STW time. The CMS processing process is more complicated. As for ParNew, it is actually approximately equal to PS. If you pay attention to the top picture, you will find that the young PS cannot be combined with CMS. So there is an extra ParNew.


Introducing the CMS stage


CMS, the full name Concurrent Mark Sweep, Chinese interpretation of concurrent mark sweeping , from the name can be seen that the algorithm idea uses the mark sweeping algorithm, let's take a look at CMS to simplify the process.

image.png

  • Initial mark. Only mark the first node reachable by the GC root . There will be a short STW.
  • Concurrent marking. The user thread and the garbage thread are performed at the same time. The garbage thread will continue to look for GCroot down, and there will be no STW. But there are also two problems.
    • Multi-label: It was not garbage before, but now the thread popping reference is disconnected and turned into garbage. Also called floating garbage.
    • Wrong label: It has been labeled as garbage before, but now it is re-quoted.
  • Remark. STW time is generally less than 200 milliseconds.
  • Concurrent cleanup. During concurrent cleanup, because the user thread and the garbage thread work together, if the CMS thread is abnormal, it may trigger the SO single-threaded execution. The procedure may be particularly slow.

Disadvantages: severe fragmentation.


to sum up


Mainly briefly introduces the generational garbage collector, especially the cms execution process, G1 will let you talk about it again. Well, some parts of the article are still unclear. I hope you can correct and comment. If you like, please like and pay attention. Pay attention, don’t get lost, I’m called Lian, while calling while practicing, the public account is [ Calling ] , and the WeChat account is [jiaolian123abc] . I wish you all a happy life.


Guess you like

Origin blog.51cto.com/14883474/2679981