JVM in action: garbage collectors and their applicable scenarios

insert image description here

garbage collector

insert image description here
The figure shows seven collectors that act on different generations. If there is a connection between the two collectors, it means that they can be used together. In JDK8, the two combinations of Serial+CMS and ParNew+Serial Old were declared obsolete, and the support of these combinations was completely canceled in JDK9

insert image description here
Parallelism and concurrency are technical terms in concurrent programming. In the context of talking about garbage collectors, they can be understood as

Parallel (Parallel) : Refers to multiple garbage collection threads working in parallel, but the user thread is still in a waiting state at this time

Concurrent : refers to the simultaneous execution of user threads and garbage collection threads

Serial collector

Serial collector

Young generation, mark-copy algorithm, single thread. When garbage collection is in progress, all other worker threads must be suspended until it finishes collecting
insert image description here

Serial Old collector

Old generation, mark-collate algorithm, single-threaded, is the old generation version of the Serial collector

There are 2 benefits as follows

  1. Works with the Parallel Scavenge collector in JDK5 and earlier
  2. As a backup plan when the CMS collector fails, it is used when Concurrent Mode Failure occurs in concurrent collection.

Parallel collector

Parallel Scavenge Collector

Young Generation, Tag Copy Algorithm, Multithreading, Mainly Focused on Throughput

Throughput = time to run user code / (time to run user code + time to run garbage collection)

Parallel Old collector

Old generation, mark-sort algorithm, multi-threaded, is the old generation version of the Parallel Scavenge collector

In cases where throughput is important or processor resources are scarce, the combination of Parallel Scavenge and Parallel Old collector can be given priority.
insert image description here

ParNew collector

ParNew is essentially a multithreaded parallel version of the Serial collector
insert image description here

CMS collector

Old age, mark-and-sweep algorithm, multi-threading, main focus on latency

The operation process is divided into 4 steps

  1. Initial mark (CMS initial mark)
  2. Concurrent mark (CMS concurrent mark)
  3. Remark (CMS remark)
  4. Concurrent sweep (CMS concurrent sweep)

insert image description here
5. Initial mark: mark the objects that GC Roots can directly associate with, which is very fast (STW will occur in this step)
6. Concurrent mark: The process of traversing the entire object graph from the directly associated object of GC Roots, this process is time-consuming Longer but does not need to pause the user thread, it can run concurrently with garbage collection
7. Remarking: In order to correct the marking record of the part of the object that changes the marking due to the continued operation of the user program during the concurrent marking period ( that is, the three-color marking method
8. Concurrent cleanup: Clean up and delete the dead objects judged in the marking phase. Since there is no need to move the surviving objects, this phase can also be concurrent with the user thread .

Because the combination of ParNew+CMS is currently the most commonly used, let's take a look at the problems of CMS?

1. Concurrent garbage collection leads to tight CPU resources

In the concurrent marking and concurrent cleaning phases, the garbage collection thread and the system worker thread work at the same time, which will cause the limited CPU resources to be occupied by the garbage collection thread.

2. Can't handle floating garbage

In the concurrent cleanup phase, the CMS recycles the previously marked objects, but the system has been running in this phase, and new garbage objects may be generated. This garbage object is "floating garbage", and the floating garbage can only wait until the next GC will be recycled

3. Concurrent Mode Failure causes the garbage collector to switch to SerialOld

If during the CMS garbage collection, the object that the program wants to put into the old age is larger than the available memory space, a Concurrent Mode Failure will occur, that is to say, the concurrent garbage collection fails. I recycle and you put the object into the old age, and the memory is not enough. used. At this time, the CMS will be replaced with SerialOld, and the garbage collection will be performed directly by Stop the World, and the garbage objects will be recycled at one time.

4. CMS uses a mark-sweep algorithm, which will cause a lot of memory fragmentation

G1 collector

The biggest feature of the G1 collector is that it divides the heap into multiple regions of equal size
insert image description here

G1 will also have the concept of the new generation and the old generation, but it is a logical concept, not a physical concept. A Region may belong to the new generation or the old generation. The memory areas of the new generation and the old generation are constantly changing and are automatically controlled by G1.

The garbage collector before G1, when a GC occurs, either reclaims the entire young generation or the entire old generation. G1 can be reclaimed for any part of the heap memory, and Region is the basic unit of a single reclamation

With Region as the basic unit of a single collection, a predictable pause time model can be established , that is, we can control the STW time by setting the -XX:MaxGCPauseMillis parameter. G1 completes garbage collection within this time as much as possible, and G1 Regions with large value returns will be preferentially recovered (that is, less time is spent and more garbage is
insert image description here
collected) The operation process of the G1 collector is as follows

  1. Initial mark: mark the objects that GC Roots can directly associate with, which is very fast (STW will occur in this step)
  2. Concurrent marking: The process of traversing the entire object graph from the directly related objects of GC Roots. This process takes a long time but does not need to stop user threads, and can run concurrently with garbage collection
  3. Final mark: In order to correct the mark record of the part of the object whose mark is changed due to the continued operation of the user program during the concurrent mark ( that is, the original snapshot in the three-color mark method, STW will also occur in this step)
  4. Screening and recycling: Some regions are recycled according to the pause time expected by the user. First, copy the surviving objects in the Region to be reclaimed to the empty Region, and then clean up the space of the old Region.

Different from the "mark-sweep" algorithm of CMS, G1 is a collector based on the "mark-clean" algorithm as a whole , but locally (between two regions) is based on the "mark-copy" algorithm In any case, both algorithms mean that no memory space fragmentation occurs during G1 operation, and that regular available memory can be provided after the garbage collection is completed. This feature is beneficial for the program to run for a long time. When the program allocates memory for large objects, it is not easy to trigger the next collection in advance because the continuous memory space cannot be found.

Summarize

collector Collection Objects and Algorithms collector type illustrate Applicable scene
Serial Cenozoic, Replication Algorithm single thread Simple and efficient; suitable for small memory
ParNew Cenozoic, Replication Algorithm Parallel multi-threaded collector The ParNew garbage collector is a multithreaded version of the Serial collector First choice with CMS garbage collector
Parallel Scavenge Throughput Priority Collector Cenozoic, Replication Algorithm Parallel multi-threaded collector Similar to ParNew, pay more attention to throughput to achieve a controllable throughput It is the default GC method on Server-level multi-CPU machines. It is mainly suitable for tasks that do not require much interaction in background operations.
collector Collection Objects and Algorithms collector type illustrate Applicable scene
Serial Old Old Age, Mark Collation Algorithm single thread Using the virtual machine in Client mode
Parallel Old Old Age, Mark Collation Algorithm Parallel multi-threaded collector Older version of the Parallel Scavenge collector, a corresponding combination developed to configure the throughput-oriented features of Parallel Scavenge Used in applications where throughput is important and CPU resource-sensitive
CMS Old age, mark-sweep algorithm Parallel and concurrent collectors Shorten the user thread stop time during garbage collection as much as possible; the disadvantages are: 1. Memory fragmentation, 2. More CPU resources are required, 3. The floating garbage problem requires more heap space An Internet website or B/S system that attaches importance to the corresponding speed of service, system downtime and user experience. Internet backend currently cms is the mainstream garbage collector
G1 Across young and old generations; mark sorting Parallel and concurrent collectors JDK1.7 was officially introduced, and the idea of ​​partition recycling was adopted to complete low-pause memory recycling without sacrificing throughput; predictable pause is its biggest advantage

Reference blog

[1]https://zhuanlan.zhihu.com/p/375977480
[2]https://www.jianshu.com/p/671495682e46
[3]https://www.zhihu.com/question/431264970/answer/1586591296

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/123380913