1. GC logical classification

Garbage collectors are not specified in the specification and can be implemented by different vendors and versions of JVMS.

Due to the rapid iteration of JDK versions, Java has spawned numerous GC versions to date.

By analyzing the garbage collector from different perspectives, GC can be categorized into different types.

1. Points by threads (garbage collection threads)

There are serial garbage collectors and parallel garbage collectors

1. Serial collection
  • Serial collection refers toOnly one CPU is allowed to perform garbage collection at a time, at which point the worker thread is suspended until the garbage collection is complete.
    • In fields where hardware platforms are not particularly superior, such as single-CPU processors or small application memory, serial collectors can outperform parallel and concurrent collectors. So serial reclamation is applied by default to the JVM in Client mode on the Client side
    • Parallel collectors produce shorter pause times than serial collectors on more concurrent cpus.
2. Parallel collection
  • In contrast to serial collection, parallel collection can use multiple cpus to perform garbage collection simultaneously. This improves the throughput of the application, but the parallel collection is still the same as the serial collection, which is exclusive and uses the “Stop the world” mechanism.

2. According to the way of debris treatment, it can be divided into compressed garbage collector and non-compressed garbage collector

  • The Compat garbage collector compacts the surviving objects after the collection is complete to eliminate the recovered fragments.
    • Redistribute object space using: pointer collisions.
  • Non-compressed garbage collectors do not do this.
    • Reallocate object space usage: free list.

3. By working memory range

It can be divided into young generation garbage collector and old generation garbage collector

Ii. Performance indicators of GC

  • Throughput: The percentage of total elapsed time spent running user code
    • Total elapsed time: the elapsed time of the program + the elapsed time of memory reclamation
  • Garbage collection overhead: the complement of throughput, the ratio of the garbage collection time to the total elapsed time.
  • Pause time: The amount of time a program’s worker thread is suspended while garbage collection is being performed
  • Collection frequency: How often collection operations occur relative to the execution of the application.
  • Memory footprint: The amount of memory occupied by the Java heap
  • Fast: The time an object takes from birth to being recycled.
  • Together they form an impossible triangle. The overall performance of all three will get better and better as technology advances. A good collector usually does at most two of these.
  • Of these three, time out becomes increasingly important. Because more memory footprint becomes more tolerable as hardware evolves, hardware performance improvements also help reduce the impact of collector runtime on the application, which increases throughput. Memory expansion has a negative effect on latency.
  • In brief, two main points should be taken:
    • throughput
    • The pause time

1. The throughput

  • Throughput is the ratio of CPU time spent running user code to total CPU consumption, i.e. Throughput = user code time run/(User code time run + garbage collection time)
    • For example, if the virtual machine runs for 100 minutes and garbage collection takes 1 minute, the throughput is 99%
  • In this case, applications can tolerate high pause times, so high-throughput applications have a longer time baseline and fast response is not a concern.
  • Throughput first, which means that STW has the shortest time per unit time: 0.2 + 0.2 = 0.4

2. Pause time

  • “Pause time” refers to a period of time during which the application thread is paused to allow the GC thread to execute
    • For example, a 100-millisecond pause time during GC means that no application threads are active during that 100-millisecond period. .
  • Pause time priority means keeping the time of a single STW as short as possible: 0.1+0.1 +0.1 +0.1=0.5

3. High throughput and low temporary contrast

  • High throughput is good because it gives the end user of the application the impression that only application threads are doing “productive” work. Intuitively, the higher the throughput, the faster the application will run.
  • Low pause times (low latency) are better because it is always bad from the end user’s point of view whether an application is suspended for GC or any other reason. Depending on the type of application, sometimes even a brief 200-millisecond pause can interrupt the end-user experience. Therefore, it is very important to have low large pause times, especially for an interactive application.
  • Unfortunately, “high throughput” and “low pause times” are competing goals.
    • If you choose throughput first, you will necessarily need to reduce the frequency of memory collection, but this will result in the GC needing longer pause times to perform memory collection.
    • On the other hand, if you choose low latency first, you can only perform memory reclamation frequently in order to reduce the pause time of each memory reclamation, which leads to a decrease in program throughput.
  • When designing (or using) a GC algorithm, we must determine our goals: a GC algorithm can only target one of two goals (i.e. focus only on large throughput or minimal pause times), or. Try to find a compromise.
  • Now standard: Reduce pause times when maximum throughput is first.

Garbage collector Overview

With virtual machines, there is a need for Garbage Collection. This is Garbage Collection, and the corresponding product is called Garbage Collector.

  • With JDK1.3.1 in 1999 came the Serial GC, which was the first GC in Serial mode. The ParNew garbage collector is a multithreaded version of the Serial collector
  • Parallel GC and Concurrent Mark Sweep GC were released along with JDK1.4.2 on February 26, 2002
  • Parallel GC became the HotSpot default GC after JDK6.
  • In 2012, G1 was available in JDK 1.7U4.
  • In 2017, G1 became the default garbage collector in JDK9, replacing CMS.
  • Parallel full garbage collection for G1 garbage collector in JDK10 in March 2018, implementing parallelism to improve worst-case latency.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — watershed — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

  • JDK11 was launched in September 2018. Introduced the Epsilon garbage collector, also known as the “No – 0P (No action) “collector. At the same time, the introduction of ZGC: scalable low delay garbage collector (Experimental).
  • In March 2019, JDK12 was released. Enhanced G1 to automatically return unused heap memory to the operating system. Meanwhile, Shenandoah GC: Experimental GC with low pause time is introduced.
  • JDK13 was released in September 2019. Enhanced ZGC to automatically return unused heap memory to the operating system.
  • In March 2020, JDK14 was released. Delete the CMS garbage collector. Extend ZGC on macOS and Windows

1.7 classic garbage collector

  • Serial collector: Serial, Serial Old
  • Parallel recyclers: ParNew, Parallel Avenge, Parallel Old
  • Concurrent collector: CMS, G1 (partitioning algorithm)

2.7 Classic garbage collector and garbage generation relationship

  • Cenozoic collectors: Serial, ParNeW, Parallel Insane
  • Collector: Serial Old, Parallel Old, CMS
  • Whole heap collector: G1

3. The combination of garbage collectors

  • There is a line between the two collectors, indicating that they can be used together: 8. Serial/Serial Old, Serial/CMS, ParNew/Serial Old, ParNew/CMS, Parallel Scavenge/Serial Old, Parallel Scavenge/Parallel insane Old, G1;
  • Serial Old is the backup plan for Concurrent Mode Failure of CMS.
  • (red dotted line) Due to maintenance and compatibility testing costs, the combination Serial+CMS and ParNew+Serial Old were declared obsolete in JDK 8 (JEP 173) and completely unsupported in JDK 9 (JEP214), i.e. : removed.
  • Insane and SerialOld GC. JDK 14.
  • JDK 14: Delete CMS garbage collector (JEP 363)

Why have a lot of collectors not enough? Because Java is used in many scenarios, mobile, server and so on. Therefore, it is necessary to provide different garbage collectors for different scenarios to improve the performance of garbage collection.

Although we will compare each collector, we are not trying to pick the best one. There is no one-size-fits-all, one-size-fits-all collector, and there is no one-size-fits-all collector. So we chose only the collector that was most appropriate for our specific application.

4. View the default garbage collector

  • -xx: +PrintCommandLineFlags: View command line parameters (including the garbage collector used)
  • Use the command line command: jinfo one flag Process ID for related garbage collector parameters

5. Switch the garbage collector

  • -XX:+PrintCommandLineFlags
  • -xx :+UseSerialGC: indicates that the new generation uses SerialGC and the Old generation uses Serial Old GC
  • -xx :+UseParNewGC: specifies the new generation to UseParNewGC
  • -xx :+UseParallelGC: Indicates that the new generation uses ParallelGC
  • -xx :+UseParallelOldGC: Indicates that ParallelOldGC is used in older years
    • Description: The two can activate each other
  • XX:+UseConcMarkSweepGC: indicates that the CMS GC is used in the old age. At the same time, the younger generation triggers the use of ParNew

Serial collector: Serial collector

  • The Serial collector is the most basic and oldest garbage collector. The only option to recycle the new generation before JDK1.3.
  • The Serial collector is the default new generation garbage collector in Client mode in HotSpot.
  • The Serial collector performs memory collection using a copy algorithm, Serial collection, and a “Stop the World” mechanism.
  • The collector is a single-threaded collector, but its “single-threaded” meaning is not only that it uses only one CPU or one collection thread to complete garbage collection, but also that it must suspend all other worker threads while it collects garbage until it stops The World.

1.Serial Old collector

In addition to the young generation, the Serial collector also provides the Serial Old collector for performing the Old generation garbage collection. The Serial Old collector also uses Serial collection and “Stop the World” mechanisms.

  • But the memory reclamation algorithm uses the tag – one compression algorithm.
  • Serial Old is the default Old garbage collector running in Client mode
  • Serial 0Od has two main uses in Server mode:
    • To be used in conjunction with the Insane;
    • As a back-up garbage collection solution for older CMS collectors.

2. Advantages of Serial collector

  • Simple and efficient (compared to the single-threaded collections of other collectors), the Seria1 collector naturally achieves the highest single-threaded collection efficiency in a single-CPU-constrained environment because it has no overhead of thread interaction.
    • A virtual machine running in Client mode is a good choice.
  • In a user’s desktop application scenario, the available memory is generally small (tens of MB to one or two hundred MB), garbage collection can be completed in a relatively short time (tens of ms to more than one hundred ms), and serial collector is acceptable as long as it does not occur frequently.
  • In the HotSpot virtual machine, use the -xx: +UseSerialGC parameter to specify that both young and old generations use serial collectors.
    • This is equivalent to replacing Serial GC for freshmen and Serial Old GC for seniors

3.Serial collector summary

  • This garbage collector, as you know, is no longer serial. And in the limited single-core CPU can only be used. It’s not even mono anymore.
  • For highly interactive applications, this garbage collector is unacceptable. Serial garbage collectors are generally not used in Javaweb applications.

ParNew collector: parallel collection

  • If the Serial GC is a single-threaded garbage collector in the younger generation, the ParNew collector is a multithreaded version of the Serial collector.
    • Par is short for Parallel, New: only deals with the New generation
  • There is little difference between the two garbage collectors except that the ParNew collector performs memory collection in a parallel collection manner. The ParNew collector in the younger generation also uses the copy algorithm, the “stop-the-world” mechanism.
  • ParNew is the default garbage collector for the new generation of many JVMS running in Server mode.

For the new generation, the recycling times are frequent and the parallel method is efficient.

For the old age, the number of recycling is less, using the serial way to save resources. (CPU parallel need to switch threads, serial can save the resources of switching threads)

1. Efficiency of ParNew collector

  • Since the ParNew collector is based on parallel collection, is it safe to assume that the ParNew collector will be more efficient at collecting than the Serial collector in any scenario?

    • ParNew collector runs in a multi-CPU environment. Because it can take full advantage of physical hardware resources such as multi-CPU and multi-core, it can complete garbage collection more quickly and improve the throughput of the program.
    • However, in a single CPU environment, the ParNew collector is no more efficient than the Serial collector. Although the Serial collector is based on Serial collection, because the CPU does not need to switch tasks frequently, it can effectively avoid some of the extra overhead associated with multithreaded interactions.
  • In addition to Serial Old, ParNew GC currently works with the CMS collector

  • In the program, the developer can manually specify the usage with the option “-xx: +UseParNewGC”. The ParNew collector performs a memory reclamation task. It means that the younger generation uses the parallel collector without affecting the older generation.

  • -xx: ParallelGCThreads Limits the number of threads that are enabled by default.

Parallel collector: Throughput first

The younger generation of HotSpot, in addition to having the ParNew collector based on Parallel recycling, also uses copying algorithms, Parallel recycling, and “Stop the World” mechanisms. So is Parallel collector superfluous?

  • Unlike the ParNew collector, the Parallel Scavenge collector aims to achieve a manageable Throughput. It is also known as a throughput-first garbage collector.
  • The adaptive adjustment strategy is also an important distinction between the Parallel Avenge and ParNew.

High throughput can make efficient use of CPU time to complete the program’s computing tasks as soon as possible. It is suitable for tasks that do not require much interaction in the background. Therefore, it is commonly used in server environments. For example, applications that perform batch processing, order processing, payroll, and scientific calculations.

Parallel Old collector

  • Parallel collector a Parallel Old collector for performing old-age garbage collection was provided in JDK1.6 in place of the Serial Old collector.
  • The Parallel Old collector uses a mark-to-compression algorithm, but is also based on Parallel collection and a “stop-the-world” mechanism.
  • In application throughput first scenarios, the combination of Parallel collector and Parallel Old collector performs well in Server mode memory reclamation.
  • In Java8, the default is this garbage collector.

2.Parallel garbage collector parameter configuration

  • -xx: +UseParallelGC Manually specifies the young generation to use the Parallel collector to perform memory reclamation tasks.
  • -xx: +UseParallel0ldGc Manually specifies that older generations are using parallel collection collectors.
    • It applies to the new generation and the old age respectively. Jdk8 is enabled by default.
    • One of the above two parameters is enabled by default, and the other is enabled as well. (Mutual activation)
  • -xx: ParallelGCThreads Sets the number of threads for the young generation of parallel collectors. In general, it is best to match the number of cpus to avoid too many threads affecting garbage collection performance.
    • By default, when the number of cpus is less than 8, the value Paralle lGCThreads equals the number of cpus.
    • ParallelGCThreads = 3+[5* cpu_count]/8 when the number of cpus is greater than 8
  • -xx: MaxGCPau3eMillis sets the maximum pause time for garbage collector (i.e. the time of STw).. The units are milliseconds.
    • To keep the pause time within MaxGCPauseMills as much as possible, the collector Adjust the Java heap size or some other parameter at work.
    • For users, the shorter the pause, the better the experience. But on the server side, we focus on high concurrency, overall throughput. So the server side is suitable for Parallel control.
    • Use this parameter with caution.
  • -xx: GCTimeRatio Ratio of the garbage collection time to the total time (= 1 / (N + 1)) Measures the throughput.
    • The value ranges from 0 to 100. The default value is 99, which means that the garbage collection time does not exceed 1.
    • Is somewhat contradictory to the previous -xx: MaxGCPauseMillis parameter. The longer the pause time, the Radio parameters tend to exceed the set ratio.
  • -xx: +UseAdaptiveSizePolicy To operate the Avenge avenge
    • In this mode, parameters such as the size of the young generation, the ratio of Eden to Survivor, and the age of objects promoted to the old generation are automatically adjusted to reach a balance between heap size, throughput, and pause time.
    • In cases where manual tuning is difficult, you can use this adaptive approach to specify only the maximum heap of the virtual machine, the throughput of the target (GCTimeRatio), and the pause time (MaxGCPauseMills), and let the virtual machine do the tuning itself.

CMS collector: Low latency (low pause time)

In the JDK1.5 era, HotSpot introduced a garbage collector that could almost be considered revolutionary in strongly interactive applications: CMS (Concurrent one Mark one Sweep) collector, the first truly Concurrent collector in the HotSpot VIRTUAL machine, for the first time allowing garbage collection threads to work simultaneously with user threads.

  • The FOCUS of the CMS collector is to minimize the pause time of user threads during garbage collection. The shorter the pause interval (low latency), the better the application that interacts with the user, and the better the response speed improves the user experience. At present, a large part of Java applications are concentrated on the server side of Internet sites or B/ S systems. These applications pay special attention to the response speed of services and hope to have the shortest system pause time to bring users a better experience. The CMS collector is a good fit for such applications. CMS’s garbage collection algorithm uses the tag-one cleanup algorithm and will also “stop a the world”
  • Unfortunately, CMS, as an older collector, does not work with the Parallel Insane, a new generation collector that already exists in JDK 1.4.0, so when CMS was used to collect older ages in JDK 1.5, The new generation can only choose one of the ParNew or Serial collectors.
  • Before G1, CMS was widely used. Today, there are still many systems using CMS GC.

1.CMS execution process

The whole process of CMS is more complex than the previous collector. The whole process is divided into four main stages, namely the initial marking stage, concurrent marking stage, re-marking stage and concurrent clearing stage.

  • Initial Mark phase: In this phase, all the worker threads in the program will be affected. The main task of this phase is simply to mark the objects to which GCRoots can be directly associated. Once the tag is complete it will resume all applications that were previously suspended. Threads. Because the directly related object is small, the speed here is very fast.
  • Concurrent Mark phase: The process of traversing the entire object graph from directly associated objects in GC Roots, which is time-consuming but does not require the suspension of the user thread and can be run concurrently with the garbage collection thread.
  • Re-marking phase: Because in concurrent mark phase, the program will work thread and garbage collection threads run at the same time or cross operation, thus to correct during concurrent tags, tags that changes caused by the user program continue to operate the part of object mark record, this phase of the pause time usually slightly longer than the initial mark phase, but also far shorter than the concurrent mark phase.
  • Concurrent Sweep phase: This phase removes dead objects judged by the marking phase and frees memory space. Since there is no need to move live objects, this phase can also be concurrent with the user thread.

2. Analysis of CMS characteristics

  • Because the most time-consuming concurrent marking and concurrent cleanup phases do not require pauses, the overall collection is low-pause.
  • Although the CMS collector uses concurrent collection (non-exclusive), it still performs a “stop-the-world” mechanism to suspend the worker threads in the program during its initialization and re-marking phases, but not for very long.
  • Thus, none of the current garbage collectors do not need to “Stop the World” at all, but simply keep the pause time as short as possible.
  • In addition, since the user threads are not interrupted during the garbage collection phase, you should also ensure that the application user threads have enough memory available during the CMS collection process. Therefore, the CMS collector does not wait until the old age is almost completely filled, as other collectors do. Instead, the CMS collector starts collecting when the heap memory usage reaches a certain threshold to ensure that the application still has enough space to run while the CMS is working. If the CMS is running without enough memory to meet the program’s requirements, a “Concurrent Mode Failure” occurs, at which point the virtual machine starts a fallback: the Serial 0LD collector is temporarily enabled to restart the old garbage collection, resulting in long pauses.
  • The GARBAGE collection algorithm of the CMS collector adopts the mark-clean algorithm, which means that after each collection, some memory fragments will inevitably be generated because the memory space occupied by the useless objects that perform the collection is most likely to be discrete chunks. The CMS will not be able to use the Bump the Pointer technique to allocate memory for new objects, and will only be able to select the Free List to allocate memory.

Since Mark Sweep causes memory fragmentation, why not change the algorithm to Mark Compact?

Because when concurrent cleanup is done, how do you use the memory used by the user thread? To ensure that the user thread can continue to execute, the resource it is running on is not affected. The Mark Compact is better suited for “Stop the World” scenarios.

3. The advantages of CMS

  • Concurrent collection
  • Low latency

4. The CMS

  • Memory fragmentation occurs, resulting in insufficient space for user threads after concurrent cleanup. In the case that large objects cannot be allocated, the Full GC has to be triggered early.
  • The CMS collector is very sensitive to CPU resources. In the concurrent phase, it does not cause user pauses, but it does slow down the application and reduce overall throughput by taking up a portion of the threads.
  • The CMS collector cannot handle floating garbage. A “Concurrent Mode Failure” may occur, resulting in another Full GC. Floating garbage: In the concurrent marking stage, the worker thread and garbage collection thread of the program run at the same time or cross, so if new garbage objects are generated in the concurrent marking stage, CMS will not be able to mark these garbage objects, which will eventually lead to the timely collection of these newly generated garbage objects. These previously unreclaimed memory Spaces can only be freed on the next GC.
  • Since the re-mark phase includes garbage fixes, why float garbage?
    • Relabelling is the detection of marked objects: because it is possible that the previous garbage object is referenced by another object, it needs to be relabelled (to prevent missing marks) to prevent it from being mistaken for garbage removal. Floating garbage, on the other hand, is not marked at first, but is an object that may become garbage after the concurrent marking phase. It refers to the new garbage generated.

5. Set CMS parameters

  • -xx: +UseConcMarkSweepGc manually specifies the use of the CMS collector to perform memory reclamation tasks.
    • If this parameter is enabled, a XX: +UseParNewGc is automatically enabled. ParNew (for Young area) +CMS (for old area) +Serial 0LD combination.
  • -xx: CMS1ni tiatingOccupanyFraction Sets the threshold of heap memory usage. Once this threshold is reached, reclamation starts.
    • The default value for JDK5 and earlier versions is 68, that is, when the space usage of older generations reaches 68, one is executed. CMS collection. JDK6 5 or later the default value is 92
    • If the memory growth is slow, you can set a larger threshold. A larger threshold can effectively reduce the triggering frequency of the CMS, and reduce the number of old reclaim times, which can significantly improve application performance. Conversely, if your application’s memory usage is growing rapidly, you should lower this threshold to avoid triggering the old serial collector too often. Therefore, this option can effectively reduce the number of Full GC executions.
  • – XX: + UseCMSCompactAtFullCollection is used to specify the execution of the Full after GC to compress the memory space, so as to avoid the generation of memory fragments. The problem, however, is that the pause times become longer because the memory compacting process cannot be executed concurrently.
  • – XX: CMSFullGCsBeforeCompaction set after how many times perform Full GC to compress the memory space.
  • -xx: ParallelCMSThreads Sets the number of CMS threads.
    • The default number of threads started by CMS is (ParallelGCThreads+3) /4. ParallelGCThreads is the number of threads for the young generation of parallel collectors. When CPU resources are tight, application performance can be very poor during the garbage collection phase due to the impact of CMs collector threads.

6. CMS changes in later JDK versions

  • New JDK9 feature: CMS marked as Deprecate (JEP291)
    • If you enable the CMS collector with the -xx: +UseConcMarkSweepGC parameter on a HotSpot VIRTUAL machine with JDK 9 or later, you will receive a warning that the CMS will be deprecated in the future.
  • New features for JDK14: Remove CMS garbage collector (JEP363)
    • The CMS garbage collector is removed. If -xx: +UseConcMarkSweepGC is used in JDK14, the JVM will not issue an error, just a warning message, but no exit. The JVM automatically falls back and starts the JVM in the default GC mode

G1 collector: Regionalization and generation type

Why release Garbage First (G1) GCS when we already have the First few powerful GCS? The reason is that applications are dealing with more and more large and complex businesses, more and more users, without GC can not ensure the normal operation of the application, and often the STW GC can not keep up with the actual needs, so they constantly try to optimize GC. The G1 (Garbage First) Garbage collector is a new Garbage collector introduced after Java7 update4, and is one of the most advanced achievements in today’s collector technology development. At the same time, in order to accommodate today’s expanding memory and increasing number of processors, pause time is further reduced while maintaining good throughput. The official goal for the G1 was to achieve the highest throughput (high throughput) possible with controllable latency (low pauses), thus fulfilling the burden and expectations of a “fully functional collector.”

1. Why is it called Garbage First (G1)?

  • Because G1 is a parallel collector, it divides heap memory into a number of unrelated regions (physically discontinuous). Use different regions to represent Eden, Survivor 0, Survivor 1, old age, and so on.
  • The G1 GC systematically avoids region-wide garbage collection across the entire Java heap. G1 tracks the value of garbage accumulation in each Region (the amount of garbage collection space obtained and the experience value of garbage collection time), maintains a priority list in the background, and collects garbage from the Region with the highest value according to the allowed collection time.
  • Since this approach focuses on regions where the most Garbage is collected, we gave G1 a name: Garbage First.
  • G1 (Garbage First) is a Garbage collector for server applications. It is mainly aimed at machines equipped with multi-core CPUS and large memory capacity. It meets the GC pause time with a high probability and has high throughput performance characteristics.
  • In JDK1.7 version officially enabled, removed the identity of Experimental, is the default garbage collector after JDK 9, replacing CMS collector and Parallel + Parallel 0LD combination. Oracle officially calls it a “full-featured garbage collector.”
  • CMS, meanwhile, has been marked deprecated in JDK 9.
  • G1 is not yet the default garbage collector in JDK8 and needs to be enabled using a XX: +UseG1GC.

2. Advantages of G1 garbage collector

1. Parallelism and concurrency
  • Parallelism: G1 can have multiple Gc threads working at the same time during collection, effectively leveraging multi-core computing power. At this point the user thread is STW
  • Concurrency: G1 has the ability to alternate execution with the application, so that some work can be performed at the same time as the application, so that, generally speaking, the application does not completely block during the entire reclamation phase
2. Collect by generation
  • In terms of generation, G1 is still a generational garbage collector. It differentiates the young generation from the old generation, and the young generation still has Eden and Survivor zones. However, from the structure of the heap, it does not require the whole Eden area, the young generation or the old generation to be continuous, nor does it insist on fixed size and fixed quantity.
  • The heap space is divided into regions that contain logical young and old generations.
  • Unlike previous recyclers, it takes care of both the young and the old. Compare other recyclers, either working in the younger generation or working in the older generation;
3. Spatial integration
  • CMS: “mark a clean” algorithm, memory fragmentation, a defragmentation after several Gc
  • The G1 divides memory into regions. Memory reclamation is based on region. The replication algorithm is used between regions.
  • But it is actually a Mark Compact algorithm as a whole, and both algorithms can avoid memory fragmentation. This feature helps programs run for a long time and allocate large objects without triggering the next GC prematurely because contiguity memory space cannot be found. This is especially true when the Java heap is very large.
4. Predictable pause time model (i.e. soft real time soft real time)

This is another advantage G1 has over CMS. In addition to pursuing low pauses, G1 also models predictable pause times, allowing users to explicitly specify that no more than N milliseconds should be spent on garbage collection within a time segment of M milliseconds.

  • Due to partitioning, G1 can select only part of the region for memory reclamation, which reduces the scope of reclamation, so that the occurrence of global pause can be well controlled.
  • G1 tracks the value of garbage accumulation in each Region (the size of garbage collection space and the experience value of garbage collection time), maintains a priority list in the background, and collects the Region with the highest value according to the allowed collection time each time. The G1 collector is guaranteed to achieve the highest possible collection efficiency in a limited time.
  • G1 may not be as delayed as CMS at best, but it is much better at worst than CMSGC.

3. The G1 shortcomings

  • Compared to CMS, G1 does not have a comprehensive, overwhelming advantage. For example, G1 has a higher garbage collection Footprint and overload than CMS during user program execution.
  • Empirically, CMS is more likely to outperform G1 in small memory applications, while G1 is more likely to outperform G1 in large memory applications. The balance point is between 6 and 8GB.

4. Set G1 parameters

  • -xx: +UseG1GC manually specifies the use of the G1 collector to perform memory reclamation tasks.
  • -xx: G1HeapRegionSize Sets the size of each Region. 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 Java heap size. The default is 1/2000 of the heap.
  • -xx: MaxGCPauseMillis sets the maximum Gc pause time metric that the JVM will try to achieve, but is not guaranteed to achieve. The default value is 200ms
  • -xx: ParallelGCThread Sets STW. The number of worker threads. The maximum value is 8
  • -xx: ConcGCThreads Sets the number of concurrent threads to be tagged. Set n to about 1/4 of the number of parallel garbage collection threads (ParallelGCThreads).
  • – XX: Ini tiatingHeapOccupancyPercent set trigger a concurrent GC cycle Java heap usage rate threshold value. If this value is exceeded, GC is triggered. The default value is 45.

5. Common operation steps for G1 collector

G1 was designed to simplify JVM performance tuning, and developers can tune it in three simple steps: Step 1: Enable G1 garbage collector Step 2: Set maximum heap memory Step 3: Set Maximum pause Time G1 provides three garbage collection modes: YoungGC, Mixed GC, and Full GC are fired under different conditions.

6.G1 Application scenario

  • Server – oriented applications for machines with large memory and multiple processors. (No surprise in a normal size heap)
  • The primary applications are applications that require low GC latency and have a large stack of solutions
  • For example, when the heap size is about 6GB or larger, predictable pause times can be less than 0.5 seconds; G1 ensures that each GC pause is not too long by incrementally cleaning only some regions at a time, not all of them.
  • To replace the CMS collector in JDK1.5; G1 may be better than CMS when:
    • More than 50% of the Java heap is occupied by active data;
    • The frequency of object assignment or chronological lifting varies greatly;
    • GC pauses are too long (longer than 0.5 to 1 second).
  • With the exception of G1, the HotSpot garbage collector uses built-in JVM threads to perform multi-threaded GC (thread priority low) operations
  • The G1 GC can use application threads to do the background GC work, meaning that when the JVM’s GC thread is slow, the application thread is called to help speed up the garbage collection process

7. Divide regions into parts

Using the G1 collector, it divides the entire Java heap into about 2048 independent Region blocks of the same size. The size of each Region block depends on the actual size of the heap. The whole Region block is controlled between 1MB and 32MB, and it is controlled to the NTH power of 2, that is, 1MB, 2MB, 4MB, 8MB, 16MB. 32 MB.

This can be set with a XX: G1HeapRegionSize. All regions are the same size and do not change during the lifetime of the JVM.

Although the concept of Cenozoic and oldyn is still retained, Cenozoic and oldyn are no longer physically separated; they are collections of parts of regions (which do not need to be continuous). Dynamic Region allocation enables logical continuity.

  • A region may belong to an Eden, Survivor, or Old/Tenured memory region. However, a region can belong to only one role. In the figure, E indicates that the region belongs to Eden memory region, S indicates that the region belongs to Survivor memory region, and O indicates that the region belongs to Old memory region. Blank Spaces in the figure represent unused memory space.
  • The G1 garbage collector also adds a new memory region called the Humongous memory region, shown in block H. It is used to store large objects. If the number of regions exceeds 1.5, the region is added to H.
  • The reason for setting H: Large objects in the heap are directly assigned to the old age by default, but if it is a short-lived large object, this can have a negative impact on the garbage collector. To solve this problem, G1 has a Humongous section, which is dedicated to large objects. If an H block does not fit a large object, G1 looks for contiguous H blocks to store. Sometimes you have to start the Full GC in order to find consecutive H regions. Most of G1’s behavior treats the H region as part of the old age.

8.G1 collector garbage collection process

The garbage collection process of G1 GC mainly includes the following three steps:

  • Young GC
  • Concurrent Marking in the old days
  • Mixed GC
  • (Single-threaded, exclusive, high-intensity Full GC will still exist if needed. It provides a fail-safe mechanism against GC evaluation failures, i.e., strong collection.

    Clockwise, young GC – > Young GC + Concurrent mark – > Mixed GC

1. The application allocates memory and starts the young generation reclamation process when the Eden area of the young generation is used up; G1’s young-generation collection phase is a parallel (multiple collection threads) exclusive (STW) collector. During the young generation collection period, the G1 GC suspends all application threads and starts multithreading to perform the young generation collection. Then move the surviving object from the young generation to the Survivor or the old, or possibly both.

2. When heap memory usage reaches a certain value (45% by default), the old-age concurrent marking process begins.

3. Mark the finished horse. Start the mixed recycling process. For a mixed payback period, the G1 GC moves live objects from the old period to the free period, which becomes part of the old period. Unlike the young generation, the G1 collector of the old generation does not need to recycle the entire old generation, but only scan/reclaim a small number of old regions at a time. At the same time, the old Region is reclaimed along with the young generation.

For example, a Web server with a Java process with a maximum heap memory of 4 gigabytes responds to 1500 requests per minute and allocates about 2 gigabytes of new memory every 45 seconds. G1 does a young generation collection every 45 seconds, with a heap utilization rate of 45% every 31 hours, and begins the old generation concurrent tagging process, followed by four or five mixed collections.

9. Memory sets and write barriers

  • The problem of an object being referenced by different regions (generational referencing problem)
  • A Region cannot be isolated. Objects in a Region can be referenced by objects in any Region. Do YOU need to scan the entire Java heap to determine whether an object is alive?
  • This problem also exists in other generational collectors (more so in G1)
  • Will the new generation also have to scan the old?
  • This would reduce MinorGC’s efficiency

Solutions:

  • Regardless of G1 or any other generational collector, the JVM uses a RememberedSet to avoid global scans
  • Each Region has a corresponding Remembered Set
  • Each time Reference data is written, a Write Barrier interrupts
  • Then check whether the Reference to be written refers to an object in a different Region than the Reference type.
  • If not, the related references are recorded in the Remembered Set of the Region where the reference points to the object through CardTable
  • When garbage collection is performed, add the enumeration scope of the GC root to Remembered Set. You can guarantee that no global scan will be done, and there will be no omissions

Ix. Details on G1 recovery process

1. Young GENERATION GC

  • When JVM starts, G1 prepares Eden area first, and the program continuously creates objects to Eden area during the running process. When Eden space runs out, G1 will start a young generation garbage collection process.
  • Young generation garbage collection only collects Eden (active) and Survivor (passive) regions.
  • In YGC, G1 first stops The execution of The application (Stop The World) and creates a Collection Set, which refers to The Collection of memory segments that need to be reclaimed. The Collection in The young generation reclamation process contains all memory segments in The Eden area and Survivor area of The young generation.
  • Copy algorithm (S, E–> Free region equals To To region) (S–> Find new free region as old age)

Then start the following recycling process:

  • In the first stage, the roots are scanned
    • The root refers to objects that static variables point to (class life cycle, object referenced by method area), local variables in the chain of executing method calls, etc. (method life cycle, object referenced by virtual machine stack). The root reference, along with the external reference to the RSet record, serves as the entry point for scanning the living object.
  • Phase 2, update the RSet

    Process cards in the Dirty Card queue (see remarks) and update the RSet. After this phase is complete, the RSet can accurately reflect the reference of the old generation to the object in the memory segment.
    • Dirty Card Queue: For the application’s reference assignment statement Object. field=object, the JVM performs special operations before and after to enqueue a card that holds object references in the dirty Card queue.
    • During the recycle of the young generation, G1 will process all cards in the Dirty Card Queue to update the RSet and ensure that the RSet accurately reflects the reference relationship in real time.
    • Why not update the RSet directly at the reference assignment statement? This is for the sake of performance, RSet processing requires thread synchronization, which can be very expensive, using queue performance is much better.
  • In the third stage, the RSet is processed.
    • Identify the objects in Eden that are pointed to by the old objects. The objects in Eden that are pointed to are considered alive.
  • In the fourth stage, objects are copied.
    • At this stage, the object tree is traversed, and the surviving objects in the memory segment of Eden area will be copied to the hollow memory segment of Survivor area. If the age of surviving objects in the memory segment of Survivor area does not reach the threshold, the age will be increased by 1. When the age reaches the threshold, the surviving objects will be copied to the hollow memory segment of 01D area. If Survivor space is insufficient, some data in Eden space will be promoted directly to the old space.
  • The fifth stage deals with references.
    • Handle Soft, Weak, Phantom, Final, JNI Weak etc references. Finally, the data in Eden space is empty, GC stops working, and the objects in the target memory are continuously stored without fragmentation. Therefore, the replication process can achieve the effect of memory consolidation and reduce fragmentation.

2. Young generation GC+ concurrent marking process

  • Initial marking phase: marking objects directly reachable from the root node. This phase is STW and triggers a young GC.
  • Root Region Scanning: THE G1 GC scans the old Region objects that are directly reachable from the Survivor Region and marks the referenced objects. This process must be completed before the Young GC.
  • Concurrent Marking: Concurrent Marking (and application execution) throughout the heap, which can be interrupted by the Young GC. During the concurrent marking phase, if all objects in a region object are found to be garbage, the region is immediately reclaimed. At the same time, the object activity (the percentage of living objects in the region) of each region is calculated during concurrent tagging.
  • Remark: Because the application is ongoing, the result of the last mark needs to be corrected. Is the STW. G1 uses a faster initial snapshot algorithm than CMS: Snapshot one at one the Beginning (SATB).
  • Exclusive cleanup (STW) : Calculates the percentage of live objects and GC collections for each region and sorts them to identify areas that can be mixed for collection. Set the stage for the next phase. Is the STW.
    • This phase does not actually do garbage collection
  • Concurrent cleanup phase: Identify and clean up completely free areas.

3. Mixed recycling

When more and more objects are promoted to the old Oldregion, in order to avoid running out of heap memory, the virtual machine triggers a Mixed garbage collector, namely Mixed GC. This algorithm is not an OldGC, but will reclaim the whole Young Region as well as part of 0ldRegion. Note here: part of the old era, not all of it. You can choose which OldRegion to collect, allowing you to control how long garbage collection takes. Also note that Mixed GC is not a Full GC.

  • After the concurrent marking ends, the segments that are 100% garbage in the old age are reclaimed and the segments that are partially garbage are calculated. By default, these older memory segments are collected eight times (with a XX: G1MixedGCCountTarget setting).
  • The Collection Set of a mixed Collection consists of one-eighth of old age segments, Eden segment, and Survivor segment. The algorithm of hybrid collection is exactly the same as the algorithm of young generation collection, but it collects more memory segments of the old generation. Please refer to the young generation recycling process above for details.
  • Since memory segments are recycled eight times by default in older generations, G1 prioritises memory segments with more garbage. The higher the percentage of garbage in memory segments, the more garbage will be collected first. And has a threshold value will determine whether memory segments are recycled, a xX: G1MixedGCLiveThresholdPercent, the default is 65%, mean waste of memory block to achieve 65% can be recycled. If the garbage ratio is too low, it means that there is a high percentage of live objects, which will take more time to replicate.
  • Mixed recycling does not have to be done eight times. There is a threshold of Xx: G1HeapWastePercent, which defaults to 10%, meaning that 10% of the total heap memory is allowed to be wasted, meaning that if the percentage of garbage that can be recycled is less than 10% of the heap memory, no mixed recycling is done. Because GC takes a lot of time but recycles very little memory.

4.Full GC

The G1 was designed to avoid Full GC. But if that doesn’t work, G1 stops application execution (Stop The World) and uses a single-threaded memory reclamation algorithm for garbage collection, with poor performance and long application pauses. To avoid Full GC, you need to adjust once it happens. When will Full GC happen? For example, if the heap is too small, G1 will fall back to full GC when there is no empty segment available for copying live objects, a situation that can be resolved by increasing memory. There are two possible causes of G1Full GC:

  • Evacuation does not have enough to a space to hold the object of advancement;
  • Space runs out before the concurrent processing completes.

5. Optimization suggestions for G1 collector

  • Young generation size
    • Avoid explicitly setting the young generation size with options such as one Xmn or one XX: NewRatio
    • Fixed the size of the young generation to override the pause time target
  • Don’t be too strict with your pause time goals
    • The throughput goal for the G1 GC is 90% application time and 10% garbage collection time
    • When evaluating G1 GC throughput, don’t be too harsh with pause time goals. Being too strict with your goals indicates that you are willing to incur more garbage collection overhead, which has a direct impact on throughput.

A summary of seven classic garbage collectors

1. How to choose garbage collector

  • The configuration of the Java garbage collector is an important choice for JVM optimization, and choosing the right garbage collector can make a big difference in JVM performance.
  • How do I choose a garbage collector?
    • Prioritizing the heap size allows the JVM to adapt.
    • If memory is less than 100M, use a serial collector
    • If it is a single-core, single-machine program, and no pause time requirements, serial collector
    • If it is multi-CPU, requires high throughput, and allows pause times of more than 1 second, choose parallelism or the JVM’s choice
    • If you have multiple cpus and are looking for low pause times that require fast responses (such as a delay of no more than 1 second, as in Internet applications), use a concurrent collector
    • The G1 is officially recommended for high performance. The current Internet projects are basically using G1.
  • Finally, we need to clarify one point:
    • There is no best collector, and there is no universal collection;
    • Tuning is always for specific scenarios, specific needs, and there is no one-size-fits-all collector

11. Revolutionary ZGC

ZGC is highly similar to Shenandoah’s goal of achieving low latency that limits garbage collection pauses to less than 10 milliseconds at any heap size with as little impact on throughput as possible.

The ZGC collector is a garbage collector based on Region memory layout, with (for now) no generation, using techniques such as read barriers, dye Pointers, and memory multiple mapping to implement concurrent marker-compression algorithms, with low latency as the primary goal.

The working process of ZGC can be divided into four stages: concurrent marking, concurrent preparatory reallocation, concurrent reallocation, concurrent remapping, etc.

ZGC is executed almost everywhere concurrently, except for the sTW that is initially marked. So the pause time is almost spent on the initial tag, the actual time for this part is very small.

In the pause time test, which the ZGC is strong at, it pulls away two orders of magnitude from the Parallel and G1 without mercy. The ZGC has no trouble keeping average pauses, 958 pauses, 998 pauses, 99.98 pauses, and maximum pause times under 10 milliseconds.