JVM garbage collector (Part 1)-CMS collector | More challenges in August

Introduction: To understand the garbage collector before you need to understand the garbage collection algorithm, if you do not know the garbage collection algorithm, click on the JVM garbage collection algorithm to learn more

Serial collector

Serial garbage collector serial garbage collector serial garbage collector serial garbage collector serial garbage collector serial garbage collector serial garbage collector serial garbage collector serial garbage collector Not only does it have only one thread doing garbage collection, but it also has to stop the other worker threads, the STW, until the garbage collection is complete.

Garbage collection algorithm: the new generation adopts the copy algorithm, the old era adopts the mark-collation algorithm.

STW: Stop The World In the process of GC, there will be a feeling of application pause, which can be understood as a kind of STW process that we usually encounter. The working thread of the whole program is stopped, and you cannot get any response to your request, as if the program has stopped running. This pause is called STW.

A brief mention of the older version of the Serial collector called Serial Old is an older version of the Serial collector, which is also a “single-threaded” garbage collector. It is also single-threaded, so there are fewer scenarios to use. It is currently used in versions prior to JDK1.5 with Parallel Use the Scavenge collector. The Scavenge collector is the JDK1.8 exploiture, 1.7 or 1.6, and can be ignored as well as the Serial application when a concurrent mode failure occurs during the CMS collector phase Old collector as a backup.

Parameter Settings: -xx :+UseSerialGC -xx :+UseSerialOldGC

Parallel avenge

The Parallel Exploiter is the application of the Serial collector, which is a multithreaded version of the Serial collector. This is because it is similar to the Serial collector in other ways than collecting garbage with multiple threads, such as garbage collection algorithms, parameter Settings, and so on. The Parallel Avenge avenge is the insane amount of time the USER's code is run on the CPU as a percentage of the total CPU consumption. The Scavenge avenge can be used to find the best throughput for your application using various parameters.

Garbage collection algorithm: the new generation adopts the copy algorithm, the old era adopts the mark-collation algorithm. -xx :+UseParallelGC(young generation) -xx :+UseParallelOldGC(old generation)

ParNew collector

The ParNew collector is not very different from the Parallel Scavenge collector. The main difference is that it can be used with the CMS collector, and is currently the only one that can be used with the CMS collector. When you specify the CMS collector, the default is to use the ParNew collector as the new generation collector

Parameter Settings: -xx :+UseParNewGC

Garbage collection algorithm: the new generation adopts the copy algorithm, the old era adopts the mark-collation algorithm.

CMS collector

The CMS collector is called a Concurrent Mark By default, the younger generation uses ParNew collector. GC thread and worker thread can be carried out at the same time, and it is through the collector with the minimum pause time (STW) as the goal. As can be seen from Mark and Sweep in CMS, mainly adopts mark-sweep algorithm In addition, the running process of the CMS collector is very different from the previous several collectors, which can be divided into four steps as follows:

  • Initial marker: Stop all non-GC threads, that is, STW, and log objects that GC ROOTs can reference directly through the reachabability analysis algorithm. This step is very fast.

  • Concurrent tags: concurrent mark phase mainly from book mark directly recorded in the GC Roots of object associations began to traverse the entire process, the process involved in the process is more complex, so take longer, but because he is a concurrent processes, GC thread and worker thread simultaneously, is not to need to pause the worker thread. The downside of this is that, because the program is always running, the state of previously marked objects changes during concurrent marking, hence the subsequent re-marking.

  • Relabeling: The relabeling stage is mainly to correct the objects whose state has changed after recording in the initial marking stage in the process of concurrent marking, and the incremental update algorithm in the three-color marking is used. STW is carried out in this stage, and the pause time is slightly longer than that in the initial marking stage, but much longer than that in the concurrent marking stage.

  • Concurrent cleanup: Start the worker thread and the GC thread at the same time. The GC thread mainly cleans the unmarked area. Since this is also a concurrent process, new objects will be marked at this stage, so no processing will be done on this object.

  • Concurrent reset: Resets the marked data during this GC.

Advantages:

  1. Concurrent collection: The entire process is concurrent, except for initial marking and re-marking.
  2. Low pause: STW pauses do not occur in the other three processes except initial marking and re-marking.

Disadvantages:

  1. CPU resource problem: The CPU is too heavy, and it is easy to grab more service CPU resources.
  2. Floating garbage problem: Floating garbage generated cannot be handled. If garbage is generated during the concurrent marking and concurrency phases, it will have to wait until the next GC.
  3. Space debris problem: Due to the mark-clean algorithm used, space debris problem is generated at the end of the collection, but can be resolved by parameter setting, but will cost some performance.
  4. Concurrent mode failure: If a concurrent garbage collection is triggered during the concurrent marking or concurrent cleanup phase and the collection is not completed, a concurrent mode failure is triggered and a single-threaded collection is stopped World, and then use the Serial Old garbage collector mentioned above for collection.

Garbage collection algorithm: mark-sweep algorithm.

Use parameter Settings: -xx :+UseConcMarkSweepGC

Other parameter Settings:

  • – XX: + UseCMSCompactAtFullCollection: allows the JVM to do after the execution of the tag to remove, solve the above problem of space debris, similar to use tags – sorting algorithm.
  • -xx :ConcGCThreads: Sets the number of concurrent threads during GC
  • – XX: CMSInitiatingOccupancyFraction: old big when using percentage reaches the triggers garbage collection (default is 92)
  • – XX: + CMSParallellnitialMarkEnabled: said at the time of initial tag multithreaded execution, shorten the STW.
  • -xx :+ CMSPARallelEnabled: Multi-threaded execution during re-marking and shorten the STW.

conclusion

The Serial collector and the Parallel Scavenge collector are older collectors, and the CMS collector,G1 collector, are better collectors. Serial is special because of the older version of the Serial collector Old can be used as a backup for CMS collector in case of concurrent mode failure, while ParNew, as CMS’s partner, is responsible for the collection of the new generation, while CMS is responsible for the collection of the Old age.

Q.E.D.