JVM garbage collection (primary)

table of Contents

 

Summary of garbage collection

1. Serial GC (copy algorithm)

2. Serial Old GC (marking-sorting algorithm)

3. ParNew GC (copy algorithm)

4. Parallel Scavenge GC (replication algorithm)

5. Parallel Old GC (marking-sorting algorithm)

6. CMS GC (mark-clear algorithm)

6.1 Initial Mark

6.2 Concurrent Mark (concurrent mark)

6.3 Concurrent Preclean (Concurrent Preclean)

6.4 Final Remark (Final Mark)

6.5 Concurrent Sweep (Concurrent Sweep)

6.6 Concurrent Reset (concurrent reset)

7. G1 GC (regional generational algorithm)

Why is it called Garbage First?

Parameter setting of G1 collector

How to set

scenes to be used


Summary of garbage collection

1. Serial GC (copy algorithm)

The Serial collector is the oldest collector. It is single-threaded and requires STW for collection. The collector uses a replication algorithm for garbage collection. The specific working principle is as follows:

 

Advantages : Simple and efficient to implement (compared with other single-threaded collectors). In a single-CPU environment, the Serial collector has no thread exchange overhead. Concentrating on garbage collection can achieve the highest single-threaded collection efficiency.

Disadvantages : relatively long delay to users

Usage scenario : Client mode; single-core server

You can use the -XX:+UseSerialGC parameter to specify that both the young generation and the old generation use Serial GC, which is equivalent to the young generation using Serial GC, and the old generation using Serial Old GC

Summary : This kind of garbage collector can be understood, the usage scenarios are basically rarely used, and it is limited to single-core CPU to play its value, but currently online are all multi-core

2. Serial Old GC (marking-sorting algorithm)

This collector is a single-threaded collector of the old age, and is the default old-age garbage collector running in Client mode. It uses the mark-sort algorithm. Serial Old has two main purposes in Server mode:

  1. Used in conjunction with the young generation Parallel Scavenge

  2. As a backup solution for the old CMS collector

    The following is the schematic diagram of the combination of Serial and Serial Old:

 

Advantages and disadvantages : see Serial GC, the difference from Serial GC is that it mainly deals with the old generation

Usage scenario : Client mode; single-core server; collocation with ParallelScavenge GC; as an alternative to CMS

3. ParNew GC (copy algorithm)

ParNew GC is the multi-threaded version of Serial GC. Par is the abbreviation of Parallel. New: means that only the young generation can be processed, so the collector is a parallel collector of the young generation. This collector uses a replication algorithm

The following is the process diagram of the recycler combining ParNew and Serial Old:

Advantages : Multi-threaded processing can use CPU resources more efficiently, although it is more efficient than Serial

Disadvantages : In a single CPU environment, the CPU needs to switch tasks frequently, resulting in additional overhead

Use scenario : multi-core server; used with CMS recycler; used with Serial Old

Note: When configuring -XX:+UseConcMarkSweepGC to select CMS as the old generation collector, the young generation defaults to ParNew, or you can use -XX:UseParNewGC to specify the use of PerNew as the young generation collector

-XX:ParallerGCThreads limits the number of threads, the number of threads with the same CPU data is enabled by default

 

4. Parallel Scavenge GC (replication algorithm)

Parallel ScavengeGC is also recycled in parallel, and also uses a replication algorithm. It differs from ParNew in the following two points:

  1. The goal of Parallel Scavenge is to achieve a controllable throughput, which can be called a throughput-first collector

  2. Adaptive adjustment strategy

The following is a schematic diagram of the combination of Parallel Scavenge GC and Parallel Old:

Advantages : controllable throughput; with adaptive adjustment strategy (but what is this adaptive adjustment strategy?)

Disadvantages : focusing on throughput leads to low processing efficiency and delay in response

Applicable scenarios : suitable for scenarios where the back-end applications have low requirements for interaction, and perform batch processing; order processing; salary payment; scientific computing applications, etc.

 

5. Parallel Old GC (marking-sorting algorithm)

The old parallel collector is also throughput-first. It is the old version of the Parallel Scavenge collector;

Usage scenario : Use with Parallel Scavenge collector; focus on throughput. jdk7 and jdk8 use this collector as the age collector by default, and use -XX:+UseParallelOldGC to specify the Paralle Old collector.

6. CMS GC (mark-clear algorithm)

The full name of CMS is: Concurrent Mark Sweep, which is a collector with the lowest possible latency. It is the first strict GC that is processed in parallel with user threads, and the focus is on recycling old GCs.

The CMS collection process is as follows:

6.1 Initial Mark

This stage is accompanied by STW suspension. The goal of initial marking is to mark all root objects, including objects directly referenced by the root object, and objects referenced by all surviving objects in the young generation (the old generation is recycled separately).

6.2 Concurrent Mark (concurrent mark)

In this stage, CMS GC traverses the old age and marks all the surviving objects, starting from the root object found in the previous stage "Initial Mark". The "concurrent marking" phase is the phase that runs simultaneously with the application without pause.

6.3 Concurrent Preclean (Concurrent Preclean)

This stage is also executed concurrently with the application thread, and there is no need to stop the application thread. Because the previous stage [Concurrent Marking] runs concurrently with the program, some references may have changed. If the reference relationship changes during the concurrent marking process, the JVM will mark the changed area as a "dirty" area through the "Card" method, which is called Card Marking.

6.4 Final Remark (Final Mark)

The final marking phase is the second (and last) STW pause in this GC event. The goal of this phase is to complete the marking of all surviving objects in the old age. Because the previous pre-cleanup phase was executed concurrently, it is possible that the GC thread cannot keep up with the modification speed of the application. So a STW pause is needed to handle various complicated situations. Usually CMS will try to execute the Final Remark phase when the young generation is as empty as possible, so as not to trigger multiple STW events in a row.

6.5 Concurrent Sweep (Concurrent Sweep)

This stage is executed concurrently with the application, no STW pause is required. The JVM deletes objects that are no longer used at this stage and reclaims the memory space they occupy.

6.6 Concurrent Reset (concurrent reset)

This stage is executed concurrently with the application program to reset the internal data related to the CMS algorithm to prepare for the next GC cycle.

 

The CMS garbage collector has done a lot of complex and useful work in reducing the pause time. The concurrent thread for garbage collection is executed without the need to suspend the application thread. Of course, CMS also has some shortcomings. The biggest problem is the memory fragmentation of the old age (because it is not compressed). In some cases, GC will cause unpredictable pause times, especially when the heap memory is large.

Advantages : concurrent collection; low latency

Disadvantages : very sensitive to CPU resources; unable to handle floating garbage; there are many memory fragments

Applicable scenarios : multi-core; low-latency scenarios

Summary :

If you want to minimize the use of memory and parallel overhead, please choose Serial Old (old generation) + Serial (young generation). If you want to maximize the throughput of your application, please choose Parallel Old (old generation) + Parallel (young generation). ) If you want to minimize the low latency of GC, please choose CMS (old generation) + ParNew (young generation)

After JDK 10+, CMS GC will be replaced by G1

7. G1 GC (regional generational algorithm)

Since there are already several powerful GCs, why release G1 GC?

The reason is that the application corresponding to the business is becoming larger and more complex, and more and more users, and the STW in the above-mentioned total GC cannot keep up with the actual demand, and each GC has relative shortcomings and cannot keep up with the low latency. Requires small and high throughput, all need to continuously optimize GC, and G1 GC is a product of the optimization process

The official goal for G1 is to achieve as high throughput as possible under controllable delays. It is a collector for server-side applications that can make full use of multi-CPU environments.

Why is it called Garbage First?

Because G1 is a parallel collector, it divides the heap memory into many unrelated regions (Regions) (physically discontinuous). Use different regions to represent Eden, survivor, old, etc. G1 tracks the value of garbage accumulation in each region and maintains a priority list in the background. Each time according to the allowable collection time, the region with the highest value is first recycled. Because this method focuses on the region with the largest amount of garbage collected, it Named junk first

Advantages : parallel and concurrent colleagues; generational collection; predictable STW time model;

Disadvantages : Small memory, CMS performance is higher than G1; large memory (6~8G) G1 is better

Parameter setting of G1 collector

-XX:+UseG1GC turns on G1GC

-XX:G1HeapRegionSize sets the size of each Region, the value is a power of 2, and the range is 1MB to 32MB. The goal is to divide about 2048 regions according to the minimum Java heap size, and the default is 1/2000 of the heap memory.

-XX:MaxGCPauseMills set the expected maximum GC pause time, the default is 200ms

-XX:InitiatingHeapOccupancyPercent sets the java heap occupancy threshold that triggers the GC cycle, if it exceeds, triggers GC, the default is 45

-XX:ParallerGCThread sets STW worker threads, up to 8

-XX: ConcGCThreads sets the number of concurrently marked threads, set about 1/4 of the number of CPUs

How to set

The design principle of G1 is to simplify JVM performance tuning, which can be completed in three simple steps: Step 1: Enable the G1 garbage collector (-XX:+UseG1GC) Step 2: Set the maximum memory of the heap (-Xmx -Xms) )

Step 3: Set the maximum pause time (-XX:MaxGCPauseMillis)

scenes to be used

  1. For server-side applications, with large memory and multiple processors

  2. Need low latency

  3. When the heap size is about 6GB or greater, the predictable pause time can be less than 0.5 seconds; (G1 cleans only part of the region instead of all regions each time to ensure that the GC pause time will not be too long) .

  4. It is used to replace the CMS collector in JDK1.5. In the following situations, using G1 may be better than CMS. The java heap is occupied by active data; the frequency of object allocation or the frequency of aging changes greatly; the GC pause time is too long (More than 0.5~1 second)

  5. In the HotSpot garbage collector, in addition to G1, other garbage collectors use built-in JVM threads to perform GC multi-threading operations. G1 GC can use application threads to use GC work, that is, when the JVM GC thread processing speed is slow, the system The application will be called to help speed up the garbage collection process.

The summary description of each GC is as follows:

 

Guess you like

Origin blog.csdn.net/HashMap_Set/article/details/113004470