A common garbage collector
There are several common garbage collectors today:
Cenozoic collector:
- Serial
- ParNew
- Parallel Scavenge
Old age collector:
- Serial Old
- CMS
- Parallel Old
Heap memory garbage collector: G1
There are wires between each garbage collector to indicate that they can be used together.
Second, the new generation of garbage collector
(1) Serial collector
Serial is a single-threaded collector for the new generation that uses a replication algorithm for garbage collection. Serial does garbage collection not only with one thread, but all user threads must Stop The World while it collects.
For example, when my mother is cleaning at home, she will not let her son throw paper on the ground while cleaning, otherwise, she will make garbage and clean up the garbage again.
Serial collector and Serial Old collector are used to collect garbage. When the user threads have reached a safe point, all threads are suspended. Serial collector is a single-thread garbage collector that uses a replication algorithm to collect garbage.
Application scenario: Client mode (desktop application). Single-core server.
Serial can be selected as a Cenozoic collector with -xx :+UserSerialGC.
(2) ParNew collector
ParNew is a multithreaded version of Serial, otherwise no different from Serial. ParNew does not perform better than the Serial collector on a single-core CPU. By default, the number of garbage collection threads enabled is the same as the number of cpus. You can set the number of garbage collection threads by using -xx :ParallelGCThreads.
The following is a schematic diagram of the combination of ParNew collector and Serial Old collector for garbage collection. When the user threads are executed to a safe point, all threads are suspended. ParNew collector uses multiple threads, using the replication algorithm for garbage collection, after collection, the user threads continue to execute.
Application scenario: Multi-core server; Used with the CMS collector. When using -xx :+UserConcMarkSweepGC to select CMS as the old collector, the new collector defaults to ParNew. You can also use -xx :+UseParNewGC to specify ParNew as the new collector.
The Avenge
The Parallel Insane is a next-generation multithreaded collector, differing from the ParNew Insane, which aims to minimize the downtime of user threads during garbage collection, and the Parallel Insane, which aims to achieve a controlled throughput.
Throughput is the ratio of the amount of time the CPU takes to execute user threads to the total amount of time the CPU takes to execute user threads. For example, if the virtual machine runs for 100 minutes and garbage collection takes 1 minute, the throughput is 99%. For example, in the following two scenarios, the garbage collector collects every 100 seconds and pauses for 10 seconds, and the garbage collector collects every 50 seconds and pauses for 7 seconds. Although the latter pauses for a shorter time, the overall throughput is lower and the overall CPU utilization is lower.
You can set the maximum amount of time for the collector to complete memory collection by -xx :MaxGCPauseMillis, and you can control throughput precisely by -xx :GCTimeRatio.
Parallel collector and Parallel Old collector combined garbage collection diagram, in the new generation, when the user threads are executed to the safe point, all threads suspend execution, ParNew collector to multithreading, using the replication algorithm garbage collection work, after collection, The user thread continues to start execution; In the Old days, when all user threads had reached a safe point, all threads would suspend execution. Parallel Old collector uses multiple threads and a tag collation algorithm for garbage collection.
Application scenario: Focuses on throughput, uses CPU efficiently, requires efficient computing and does not require much interaction.
To be insane, use -xx :+UseParallelGC to select the Parallel Scavenge avenge. The jdK7, JDK8 and Jdk7 avenge avenge.
Three, the old garbage collector
(1) Serial Old collector
The Serial Old collector, an older version of Serial, is also a single-threaded collector with a mark-collation algorithm.
The Serial collector and Serial Old collector combined for garbage collection are shown below:
Application scenario: Client mode (desktop application). Single-core server; Avenge with the Parallel Scavenge; As a backup to the CMS collector.
(2) CMS(Concurrent Mark Sweep) collector
The CMS collector is a collector that aims for the shortest collection pause time, known as the “shortest user thread pause time.” The garbage collection process is divided into four steps:
① Initial tag: Tag objects that GC Roots can directly associate with faster.
(2) Concurrent marking: It takes a long time to mark all garbage objects by GC Roots Tracing.
③ Re-marking: it is time-consuming to revise the mark record of the object that changes when the concurrent marking stage leads the user program to continue running.
④ Concurrent removal: it takes a long time to remove garbage objects with the mark-removal algorithm.
The most time-consuming concurrent tagging and concurrent cleanup work with the user thread, so in general, the CMS collector garbage collection can be viewed as being performed concurrently with the user thread.
The CMS collector also has some disadvantages:
Sensitive to CPU resources: The default number of garbage collection threads allocated is (number of cpus +3) /4. As the number of cpus decreases, the more CPU resources consumed, the lower throughput
Unable to handle floating garbage: During the concurrent cleanup phase, the CMS collector is unable to remove this part of garbage in the current collection because the user thread is still running and new garbage is constantly being generated. Because the user threads are also executing concurrently during the garbage collection phase, the CMS collector cannot wait until the ages are filled, as other collectors do, and needs to reserve space for the user threads to run. When CMS is running and the reserved memory cannot meet the needs of user threads, a “Concurrent Mode Failure” will occur, and a backup plan will be initiated, temporarily using Serial Old to restart the Old garbage collection.
Because the CMS is based on the tag – clear algorithm, so after garbage collection can produce space debris, can pass – XX: UserCMSCompactAtFullCollection open defragmentation open (the default), before CMS for Full GC, to memory fragments. Still can use – XX: CMSFullGCsBeforeCompaction set execute many times without compression (without defragmentation) after a Full GC, followed by a Full GC with compression (defragmentation).
Application scenario: Server response speed and minimum system pause time are required. You can use -xx :+UserConMarkSweepGC to select the CMS as the old-age collector.
(3) Parallel Old collector
The Parallel Old collector is an older version of the Parallel Avenge, a multi-threaded collector that uses a mark-collation algorithm. Can be used with the Parallel Exploiter to exploit the computing power of multi-core cpus.
Application scenarios: Use with the Parallel Exploiter; Focus on throughput. By default, jdk7 and JDK8 use this collector as the old-age collector. Use -xx :+UseParallelOldGC to specify the use of Paralle Old collector.
New generation and old age garbage collector
G1 collector
The G1 collector was officially referenced commercially in jdk1.7 and is now the default collector in jdk9. G1 collects garbage from the entire heap. It divides the whole heap into multiple independent regions of equal size. The concept of new generation and old age is retained in G1 collector. They are all part of regions, as shown in the following figure:
Each square is a region, and each region may be Eden, Survivor, or old age, or the number of each region may not be the same. JVM startup automatically sets the size of each region (1M ~ 32M, must be a power of 2), can be set to a maximum of 2048 regions (that is, the maximum supported heap memory is 32M*2048 = 64G), if set to -xmx8g-xMS8g, Then the size of each region is 8g/2048=4M.
In order to avoid scanning the whole heap when GC Roots Tracing, there is a Remembered Set in each Region to record the reference relationship between the reference type data in this Region and the reference data in other regions in real time. There is also a Remembered Set for the new generation and old generation to record references to other regions in real time), and reference these references directly when marking to see if objects should be cleared without scanning the whole heap.
The G1 collector can “model predictable pause times” by maintaining a list of the value of each Region’s collection (the amount of space acquired after collection and the empirical value of the time it took to collect) to ensure that the G1 collector achieves maximum collection efficiency in a limited amount of time.
As shown in the following figure, the G1 collector has an initial mark, concurrent mark, final mark, and filter collection process, which is similar to the previous steps of the CMS collector:
(1) Initial mark: mark objects directly associated with GC Roots. This stage is faster and requires the user thread to be stopped and executed by a single thread.
(2) Concurrent marking: Starting from GC Root, reachable objects in the heap are analyzed to find viable objects. This phase is time-consuming, but can be performed concurrently with the user thread.
(3) Final mark: correct the mark record that changes during the concurrent mark stage caused by the execution of user program.
④ Screening and recycling: In the filter collection phase, the collection value and cost of each Region are sorted, and the collection plan is specified according to the expected GC pause time of the user. In order to improve the recycling efficiency, the user thread is not executed concurrently with the user thread. Instead, the user thread is paused.
Application scenario: GC pause time should be controllable as much as possible. Applications with large memory usage. The G1 collector can be used with -xx :+UseG1GC; jdK9 uses the G1 collector by default.
JVM garbage collector Summary
This article mainly introduces garbage collector in JVM, including serial collector, parallel collector, CMS collector, G1 collector. They all have their pros and cons, and in general you need to do performance tests based on the garbage collector, depending on your business, before you make a choice. The following are the parameters that are often used to configure the collector:
-xx :+UseSerialGC: Use serial collector in new generation and old generation
-xx :+UseParNewGC: Use the parallel collector in the new generation
-xx :+UseParallelGC: The new generation uses the parallel collection collector, focusing more on throughput
-xx :+UseParallelOldGC: Old age to use the parallel collection collector
-xx :ParallelGCThreads: Sets the number of threads used for garbage collection
-xx :+UseConcMarkSweepGC: The new generation uses the parallel collector, and the old generation uses CMS+ serial collector
-xx :ParallelCMSThreads: Sets the number of CMS threads
-xx :+UseG1GC: enables G1 garbage collector
The JVM series:
In-depth analysis of JVM memory regions and memory overflow analysis
JVM to determine whether an object is dead and four garbage collection algorithms
The JVM configures common parameters and common GC tuning policies
7 JVM garbage collector features, Pros and cons, and usage scenarios!
The last
The follow-up will continue to update the performance optimization knowledge, write bad places also hope Daniel can give advice, we feel good can point a thumbs-up in the attention, will share more articles!