2. Java Garbage Collector

The garbage collection algorithm is the methodology of memory recovery, and the garbage collector is the specific implementation of memory recovery. The figure below shows 7 different generations of collectors. If there is a connection between the two collectors, it means that they can be used together. There is no such thing as the best collector, what we need to choose is the most suitable collector for the specific application.

1) Serial garbage collector (single thread, copy algorithm) -- the new generation
    is the most basic garbage collector, using the copy algorithm, which used to be the only garbage collector in the new generation before JDK1.3.1. Serial is a single-threaded collector. It not only uses one CPU or one thread to complete the garbage collection work, but also must suspend all other worker threads while performing garbage collection until the garbage collection ends. Although the Serial garbage collector needs to suspend all other worker threads during the garbage collection process, it is simple and efficient. For a limited single CPU environment, there is no thread interaction overhead, and the highest single-threaded garbage collection efficiency can be obtained. Therefore, Serial garbage The collector is still the default new generation garbage collector for the java virtual machine running in Client mode.

2) ParNew Garbage Collector (Serial+Multithreading)--The new generation
ParNew collector is actually a multithreaded version of the Serial collector. It is the preferred new generation collector for many virtual machines running in Server mode , because in addition to Serial Except for the collector, currently only it can work with the CMS collector, so it is the default garbage collector for the new generation of many java virtual machines running in Server mode.

In addition to using multi-threading for garbage collection, the ParNew garbage collector behaves exactly the same as the Serial collector. The ParNew garbage collector also suspends all other worker threads during garbage collection.

The ParNew collector opens the same number of threads as the number of CPUs by default, and the number of threads of the garbage collector can be limited by the -XX:ParallelGCThreads parameter. [Parallel: Parallel]

3) Parallel Scavenge collector (multi-threaded copy algorithm, high efficiency) -- new generation

The Parallel Scavenge collector is also a new generation garbage collector. It also uses the copy algorithm and is also a multi-threaded garbage collector. It focuses on the program to achieve a controllable throughput (Thoughput, the time the CPU is used to run user code /CPU total consumption time, that is, throughput = running user code time/(running user code time + garbage collection time)), high throughput can make the most efficient use of CPU time, and complete the calculation tasks of the program as soon as possible, mainly applicable to Tasks that operate in the background without requiring much interaction. The adaptive tuning strategy is also an important difference between the ParallelScavenge collector and the ParNew collector.

4) CMS collector (multithreaded mark-and-sweep algorithm)

It is a collector whose goal is to obtain the shortest collection pause time. Advantages: concurrent collection, low pause . Based on the "mark-and-sweep" algorithm. At present, a large part of Java applications are concentrated on the server side of the Internet website or B/S system. This type of application pays special attention to the response speed of the service. It is hoped that the system pause time will be the shortest, so as to bring a better experience to users. CMS collector It is very suitable for the needs of such applications. The operation process is more complicated and is divided into 4 steps:

shortcoming:

It is very sensitive to CPU resources , and programs designed for concurrency will be more sensitive to CPU resources. CMS default recycling thread number: (CPU number+3)/4

Unable to handle floating garbage , "Concurrent Mode Failure" may fail and cause another Full GC to occur. The garbage generated by the user program running in the concurrent cleaning phase has passed the marking phase and cannot be cleaned up in this collection, which is called floating garbage. By default, the CMS collector is activated after 68% of the space in the old generation is used. If the old generation is not growing very fast, you can properly increase the parameter -XX:CMSInitiatingOccupancyFraction to increase the trigger percentage, but if it is too high, it will easily lead to "Concurrent Mode Failure" failure.

Based on the "mark-and-sweep" algorithm, a large amount of space debris will be generated . The switch parameter -XX:+UseCMSCompactAtFullCollection is provided for the defragmentation process after the Full GC service, and the process of memory defragmentation cannot be concurrent. But the pause time will become longer.

-XX:CMSFullGCsBeforeCompation sets the number of Full GCs without compression, followed by one with compression.

 5. G1 collector (supports both new generation and old generation)

The Garbage first garbage collector is the most cutting-edge achievement in the development of garbage collector theory. Compared with the CMS collector, the two most prominent improvements of the G1 collector are:

1. Based on the mark-sort algorithm, no memory fragmentation occurs.
2. Pause time can be controlled very precisely, and low-pause garbage collection can be achieved without sacrificing throughput.
The G1 collector avoids full-area garbage collection. It divides the heap memory into several independent areas of fixed size, and tracks the
progress of garbage collection in these areas. At the same time, it maintains a priority list in the background, each time according to the allowed collection time,
The areas with the most garbage are collected first . The area division and priority area recycling mechanism ensures that the G1 collector can obtain the highest garbage collection
efficiency within a limited time.

Guess you like

Origin blog.csdn.net/lzzyok/article/details/121324789