This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

Garbage collector performance metrics

  • Throughput: The ratio of program run time to total run time (total run time = program run time + garbage collection time), the less garbage collection time, the higher throughput;
  • Pause time: STW time;
  • Memory footprint: The size of the Java heap.

The above three points form an impossible triangle, meaning that it is impossible for a garbage collector to satisfy all three. As hardware advances, memory footprint becomes less of a concern and we focus on throughput and pause times when evaluating garbage collector performance. Throughput and pause times are contradictory, and the current goal is to reduce pause times with maximum throughput first.

History of garbage collector development

  • In 1999, the first Serial GC was released in JDK 1.3.1. The ParNew garbage collector is a multithreaded version of Serial collector.
  • On February 26, 2002, Parallel GC and Concurrent Mark Sweep GC (CMS) were released along with JDK 1.4.2;
  • Parallel GC was called HotSpot default GC after JDK 1.6;
  • In 2012, G1 was available in JDK 1.7 U4;
  • In 2017, G1 became the default garbage collector in JDK 9, and CMS was marked as obsolete;
  • In March 2018, G1 parallelism was improved in JDK 10;
  • In September 2018, JDK 11 introduced the Epsilon garbage collector, along with ZGC (experimental version);
  • In March 2019, JDK 12 was released, enhancing G1 and introducing Shenandoah GC (experimental version);
  • In September 2019, JDK 13 was released to enhance ZGC;
  • In March 2020, JDK 14 was released to remove CMS and expand ZGC applications on MAC and Windows.

Garbage collector combination

7 classic garbage collector combinations:

Description:

  1. There is a wire between the two recyclers, indicating that they can be used together;
  2. Serial Old is a backup plan for Concurrent Mode Failure of CMS.
  3. G1 can be used in Cenozoic and old age;
  4. Red dotted line: JDK 8 declared these two groups obsolete and removed them completely in JDK 9;
  5. Dotted green line: deprecated in JDK 14;
  6. Green dotted border: IN JDK 14, CMS was removed.

Default garbage collector view

Write a simple Java program:

public class Test {
    public static void main(String[] args) {
        System.out.println("hello"); }}Copy the code

Add -xx :+PrintCommandLineFlagsJVM parameter configuration, in JDK 8 program output:

-XX:InitialHeapSize=536870912 -XX:MaxHeapSize=8589934592 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
hello
Copy the code

-xx :+UseParallelGC indicates that the default garbage collector in JDK 8 is Parallel.

Output in JDK 9:

-XX:G1ConcRefinementThreads=10 -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=8589934592 -XX:+PrintCommandLineFlags -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC 
hello
Copy the code

-xx :+UseG1GC indicates that the default garbage collector in JDK 9 is G1.

Classic garbage collector introduction

Serial, Serial Old collector

Serial garbage collector is a single-thread Serial garbage collector, which is the default new-generation garbage collector in HotSpot Client mode. It adopts replication algorithm, Serial garbage collector and STW mechanism to collect memory.

Serial Old garbage collector Serial Old garbage collector for Serial, using a tag compression algorithm, Serial collection, and STW mechanism for memory collection:

  • Serial Old is the default Old garbage collector running in Client mode.
  • The Serial Insane is used in two applications under the Server model: to be used in conjunction with the next-generation Parallel Insane; As a back-up garbage collection solution for the CMS collector of the old days.

Serial is applicable to VMS running in Client mode or environments with small memory (tens of MB to one or two hundred MB). Because Serial is used and has a long STW, it is not suitable for applications that require fast response and strong interaction.

Serial collector can be enabled by XX:+UseSerialGC parameter, indicating that the new generation uses Serial and the Old generation uses Serial Old.

ParNew collector

ParNew, short for Parallel New, is the multithreaded version of Serial garbage collector. ParNew is the default garbage collector for many JVMS running in Server mode, using replication algorithms, parallel collection, and STW mechanisms for memory collection.

The ParNew collector can be enabled by using the XX:+UseParNewGC parameter to indicate that the new generation uses ParNew and the old generation is not affected.

Serial, ParNew with Serial Old collector schematic diagram:

Image from codertw.com/%E7%A8%8B%E…

Parallel, Parallel Old collector

The Parallel Exploiter is also used on the new generation of the Insane, using replication algorithms, Parallel recycling and STW mechanisms.

Avenge and ParNew.

  • Parallel insane, a through-first garbage collector;
  • Parallel avenge has an adaptive adjustment strategy.

JDK 1.6 provides a Parallel garbage collector for older generations, the Parallel Old collector, to replace the Serial Old collector. Parallel uses tag compression, Parallel collection, and STW mechanisms.

The application of the Parallel Avenge avenge can be specified by -xx :+UseParallelGC. -xx :+UseParallelOldGC Specifies that ParallelOld collectors are used for older generations. They exist in pairs, enabling one collector as well as the other.

You can also set the number of concurrent collector threads by using -xx :ParallelGCThreads= :

  • By default, when the number of cpus is less than 8,-XX:ParallelGCThreads=Is equal to the number of cpus;
  • When the number of cpus is greater than 8,-XX:ParallelGCThreads=The value is equal to the3+5*CPU_COUNT/8.

-xx :+UseAdaptiveSizePolicy To unlock the Insane.

  • In this mode, the size of the young generation, the ratio of Eden to Survivor, and the age threshold of objects promoted to the old age are automatically adjusted to achieve a balance between heap size, throughput, and pause time.

CMS collector

JDK 1.5 HotSpot introduced a true Concurrent collector, CMS (concurrent-mark-sweep), which for the first time allowed garbage collector threads and user threads to work simultaneously. CMS focuses on minimizing the amount of time that user threads pause during garbage collection.

CMS is an older garbage collector and cannot be used with the Parallel Scavenge avenge. It can only be used with ParNew or Serial.

Schematic diagram of CMS collector:

Image from codertw.com/%E7%A8%8B%E…

It is mainly divided into the following steps:

  1. Initial-mark: All user thread pauses (STW). This stage only marks objects that GC Roots can be directly associated with, so it is very fast and STW time is very short;
  2. Concurrent-mark: This phase traverses the entire object chain from the GC Roots directly associated object. Although this process takes a long time, it does not suspend the user thread and executes concurrently, without STW.
  3. Remarking: This step is used to correct the mark record for the part of the object whose mark changed because the user thread continued running, since the previous step was also executed by the user thread. This phase takes a little longer than the initial tagging phase, but much less than the concurrent tagging phase;
  4. Concurrent-sweep: This phase removes garbage and reclaims space. Since no objects are moved, no STW is required for this phase.

The advantages and disadvantages of CMS are obvious:

Advantages:

  • Concurrent collection;
  • Low latency.

Disadvantages:

  • It creates debris. Since the user-line thread is still executing during the cleanup phase, only the mark-sweep algorithm without moving objects can be used, which causes fragmentation problems.
  • Sensitive to CPU resources. In addition to user threads, CPU resources need to be allocated for garbage collection, reducing throughput;
  • Unable to handle floating garbage. The concurrent marking phase, where the user thread is not stopped, also generates garbage that the CMS cannot mark and will be left for the next GC.

In addition, the CMS also needs to make sure that the user thread has enough memory available during the collection process because the user thread is not interrupted. In other words, the CMS collector cannot wait until the old age is about to fill, but should start collecting when the heap memory usage reaches a certain threshold. If the CMS runs out of reserved memory, a “Concurrent Mode Failure” will occur, and the virtual machine will start a backup plan and temporarily enable the Serial Old collector to complete the garbage collection of the Old era.

CMS collector can set parameters:

  • -XX:+UseConcMarkSweepGC, enable CMS GC.-XX:+UseParNewGCWill automatically open;
  • -XX:CMSInitiatingOccupanyFraction=, set the heap usage threshold, once this threshold is reached, CMS begins to reclaim (default value 68 for JDK5 and earlier, default value 92% for JDK6 and later);
  • -XX:+UseCMSCompactAtFullCollectionTo specify that the memory space should be compressed after the CMS reclaims the old age to avoid fragmentation.
  • -XX:CMSFullGCsBeforeCompaction, set the number of times the CMS GC is performed before the memory space is compressed.
  • -XX:ParallelCMSThreads=To set the number of CMS threads. The default number of threads started is(ParallelGCThreads+3)/4. We know that ParallelGCThreads defaults to the number of cpus when the number of cpus is less than 8, so for an 8-core CPU, the default number of CMS threads started is 3. In other words, only 62.5% of CPU resources are used to process user threads. Therefore, CMS is not suitable for scenarios with high throughput requirements.

G1 collector

The G1 (Garbage First) collector divides the heap into a number of physically discontinuous regions, using different regions to represent the Eden zone, the survivor zone, and the Old Age.

G1 avoids garbage collection for the entire Java heap. It tracks the value of garbage collection in each region (the amount of space and time required for garbage collection), maintains a priority list in the background, and prioritizes the region with the highest value for garbage collection based on the allowed collection time.

Region’s instructions

Images from tech.meituan.com/2016/09/23/…

  • E represents Eden Park, S represents survivor area, O represents old age, and blank represents unused memory area.
  • A region can belong to only one role at a time.
  • The G1 has a new memory area, Humongous, for large objects.

G1 Garbage recycling process is shown in the figure below:

Image from codertw.com/%E7%A8%8B%E…

It is mainly divided into the following steps:

  1. Initial tagging: Simply tagging objects that GC Roots can associate directly requires STW, but this process is very fast;
  2. Concurrent marking: Start from GC Roots to analyze the reacability of objects in the heap and find out the surviving objects. This stage is time-consuming, but can be executed concurrently with the user thread.
  3. Final tag: Fix the part of the object’s tag record that changes during the concurrent tag phase because the user thread continues to run.
  4. Filter collection: Sorts the collection value and cost of each region and makes a collection plan based on the expected pause time of users. This phase pauses the user thread, STW.

Advantages and disadvantages of G1 collector:

Advantages:

  • Parallelism and concurrency;
  • Generation collection, different algorithms can be used to deal with different objects;
  • Space consolidation, mark compression algorithm means no memory fragmentation;
  • The predictable pause time allows the user to specify a time segment of M milliseconds in length that will consume no more than N milliseconds in garbage collection (based on the priority list, the region with the highest value will be recycled first).

Disadvantages:

  • No advantage over CMS in a small memory environment, G1 is suitable for large heap memory;
  • G1 has a higher memory footprint for garbage collection and additional execution load than CMS during user program execution.

G1 collector related parameters Settings:

  • -XX:+UseG1GC, enable G1 GC;
  • -XX:G1HeapRegionSize=To set the region size. The value is a power of 2, ranging from 1MB to 32MB, and the goal is to partition about 2048 regions based on the minimum heap memory size. So if this value is set to 2MB, then the heap minimum is approximately 4GB;
  • -XX:MaxGCPauseMillis=, sets the maximum GC pause time that the JVM will try to achieve, but is not guaranteed to achieve. The default is 200ms.
  • -XX:ParallelGCThread=, set the value of THE GC thread when STW is set to 8 at most;
  • -XX:ConcGCThreads=ParallelGCThread (ParallelGCThread), set the number of concurrent threads to be marked. The recommended value is about 1/4 of ParallelGCThread.
  • -XX:InitiatingHeapOccupancyPercent=, sets the Java heap usage threshold that triggers concurrent GC cycles, beyond which GC is triggered. The default value is 45.

conclusion

The above several classic garbage collector has its own characteristics, the specific use of the need to choose a different garbage collector according to the specific situation:

Garbage collector classification Role position Using the algorithm The characteristics of Applicable scenario
Serial serial The new generation Replication algorithm Speed of response priority This mode applies to the Client mode in a single-CPU environment
ParNew parallel The new generation Replication algorithm Speed of response priority It works with the CMS in Server mode with multiple cpus
Parallel parallel The new generation Replication algorithm Throughput priority Ideal for scenarios where background computing does not require much interaction
Serial Old serial The old s Mark-compression algorithm Speed of response priority Client mode in a single-CPU environment
Parallel Old parallel The old s Mark-compression algorithm Throughput priority Ideal for scenarios where background computing does not require much interaction
CMS concurrent The old s Mark-compression algorithm Speed of response priority Applicable to Internet or B/S services
G1 Parallelism and concurrency New generation, old age Copy algorithm mark-compression algorithm Speed of response priority Service-oriented applications

New garbage collector

Epsilon collector, Shenandoah collector, ZGC collector