There is a "high wall" between Java and C++ surrounded by dynamic memory allocation and garbage collection techniques. People on the outside want to get in, but people on the inside want to get out ---- "understanding the Java virtual machine"Copy the code

Previous articles have covered memory regions and garbage collection algorithms in the JVM. Today, I’ll introduce their implementation – garbage collector. Over the course of the Hotspot VIRTUAL machine, different garbage collectors have been developed for various scenarios, each with its own characteristics and uses.

The figure shows seven collectors acting on different generations, and if there is a line between the two collectors, they can be used together. The region of the virtual machine indicates whether it belongs to the new generation or the old generation collector.

Cenozoic collectors: Serial, ParNew, Parallel Insane. —- all copy algorithms

Collector: CMS(mark-clean), Serial Old(Mark-copy), Parallel Old(Mark-clean)

Whole heap collector: G1 (mark-clear algorithm in one Region, copy algorithm between two regions)

Let’s start with a few nouns:

1. Parallel: Multiple garbage collection threads work in Parallel while the user thread is in a waiting state.

Concurrent: the user thread and the garbage collector thread execute concurrently.

3, throughput: run user code time /(run user code time + garbage collection time).

STW(Stop The World) : Suspends all user worker threads.

1. Serial collector

Single-threaded, new generation collector, uses replication algorithm, has STW problem

The Serial collector is the most basic and oldest in development.

Features: Simple and efficient (compared to other collectors), Serial collectors naturally achieve the highest single-threaded collection efficiency in a single-CPU-constrained environment because they have no overhead of field interaction. The problem, however, is that single threads are not sufficient for today’s multi-CPU machines, and this collector suspends all user worker threads while garbage collection is performed, which means that there are STW issues (which are not addressed by other collectors).

Application scenario: This mode is best used for VMS running in Client mode

2. ParNew collector

Multithreading, new generation collector, using replication algorithm, STW problem

The ParNew collector is a multithreaded version of The Serial collector. In addition to using multiple threads for garbage collection, The rest of The behavior is The same. The code also has a lot of reuse, including control parameters, collection algorithms, Stop The World, object allocation rules, collection strategies, etc.

ParallelGCThreads can be used to limit the number of threads that can be collected by using the -xx :ParallelGCThreads parameter. The same problem with The Serial collector is Stop The World.

Application scenario: The ParNew collector is the preferred next-generation collector for many virtual machines running in Server mode, as it is the only one besides Serial that works with the CMS collector.

Applicability. 3

GC adaptive tuning strategy, multi-threading, new generation collector, using replication algorithm, STW problem

The Parallel Scavenge collector is closely related to the throughput, so it can also be a through-first collector. Two parameters are provided to precisely control the throughput, namely to control the maximum garbage collection pause time: -xx :MaxGCPauseMillis and to directly set the throughput size: XX:GCRatio.

The Parallel Exploitinsane is designed to achieve controlled throughput and the application of the exploitinsane insane. The Parallel Exploitinsane provides a switch parameter -xx :+UseAdptiveSizePolicy to implement the GC adaptive adjustment strategy (an important difference from ParNew).

GC adaptive adjustment strategy: -xx :+UseAdptiveSizePolicy. After the switch is turned on, Don’t need to manually specify the size of the Cenozoic (Xmn), Eden and Survivor area ratio (- XX: SurvivorRation), the promotion of the old s object (- XX: PretenureSizeThreshold) parameters such as age, The VM collects performance monitoring information based on the current system running status and dynamically adjusts these parameters to provide the most appropriate pause time or maximum throughput. Also said to use PS collector easy, switch on, let the system look at their own.

4. Serial Old collector

Single thread, new generation collector, using tag collation algorithm, has STW problem

Serial Old is an older version of the Serial collector.

Features: The same single-thread collector, the use of the tag sorting algorithm, other than Serial is not much different

Run the same schematic as Serial

Parallel Old collector

GC adaptive tuning strategy, multi-threading, old-time collector, using tag collation algorithm, STW problem

Features: The Parallel Old is an older version of the Parallel Avenge collector, using multithreading and the tag collation algorithm.

Application scenarios: The Parallel Avenge +Parallel Old collector is preferred for high throughput and CPU-sensitive applications.

Run the same sketch as Parallel Insane

6. CMS collector

The old era collector, which uses the mark – clearing algorithm, has concurrent low pauses

The CMS(Concurrent Mark Sweep) collector is a collector whose goal is to obtain the shortest collection pause time.

Features: As above

Application scenario: This mode is applicable to scenarios that emphasize service response speed, minimize system pause time, and provide better user experience. Such as Web applications, B/S services.

The process that the CMS runs:

  • Initial tag: Tag objects that GC Roots can be directly associated with, which is fast but still has The Stop The World problem.
  • Concurrent marking: Go through the process of GC Roots Tracing to find live objects that can be executed concurrently by user threads.
  • Re-mark: To correct the mark record of the portion of the object whose mark changed as the user program continued to run during concurrent marking. There is still The Stop The World problem.
  • Concurrent cleanup: Cleanup of marked objects.

Concurrent marking and concurrent clearing of procedures are performed concurrently with the user thread.

Disadvantages of the CMS collector:

  • Very sensitive to CPU resources. While the concurrent phase does not suspend the user thread, it consumes some thread or CPU resources and reduces overall throughput.
  • Unable to handle floating garbage. During the Concurrent cleanup phase, when the user thread is still running, new garbage is generated and a Concurrent Model Failure may occur, resulting in another Full GC.
  • Space fragmentation is a problem with mark-clean algorithms, causing large objects to fail to allocate space and have to trigger a Full GC in advance.

7. G1 collector

Parallel concurrency, generational collection, spatial integration, predictable pauses

G1 Garbage First A Garbage collector for server-side applications. From the perspective of generation, G1 still belongs to the generation collector, including the New generation, old age, Eden area and survivor area. The G1 collector partitions the entire heap into regions, collecting only a few regions at a time to control the pause time for garbage collection. Here’s how G1 is partitioned across the heap (no longer physically, but logically)

Features:

  • Parallelism: G1 can be run by multiple GC threads simultaneously during collection, effectively leveraging multi-core computing power
  • 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, avoiding blocking the application during the entire recycle period.
  • Generational collection: G1 manages the entire Java heap on its own and works differently with newly created objects and old objects that have been around for a while and have survived multiple GC’s for better collection.
  • Space consolidation: The G1 does not generate space debris during operation and provides neat usable memory after collection.
  • Predictable pauses: In addition to pursuing low pauses, G1 models predictable pause times. Allows the user to specify explicitly in a long

No more than N milliseconds must be spent on garbage collection for a period of M milliseconds. The entire heap is divided into small areas to make it easy to control the time of collection.

The process for the G1 collector has initial marking, concurrent marking, final marking, filter collection, and the first few steps are the same as for CMS.

  • Initial tag: Tag objects that GC Roots can be directly associated with, which is fast but still has The Stop The World problem.

  • Concurrent marking: A reachable new analysis of objects in the heap, starting with GC Root, is performed to find viable objects. This phase is time-consuming, but can be performed concurrently with the user thread.

  • Final markup: Corrects markup records that change during the concurrent markup phase due to user program execution. There is a problem with paused user threads (STW).

  • Filter 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.

G1 Related Parameters

-xx :+UseG1GC Switches on G1.

-xx :ParallelGCThreads Sets the number of threads that perform memory reclamation during parallel GC.

Our usual JDK versions are still 1.7 and 1.8, and the G1 collector is already commercially available, though it is used less in the real world, as the default garbage collector since JDK 1.9.

8. Garbage collector parameters

  1. -xx :+UseSerialGC: specifies the default value when the VM runs in CLient mode. After this function is enabled, the Serial and Serial Old collectors are used to reclaim the memory.
  2. -xx :+UseParNewGC: when opened, the collector combination of ParNew + Serial Old is used for memory reclamation.
  3. -xx :+UseConcMarkSweepGC: open. The collector combination of ParNew + CMS + Serial Old is used for memory reclamation. The Serial Old collector is used as a fallback collector after a Concurrent Mode Failure occurs in the CMS collector.
  4. -xx :+UseParallelGC: Specifies the default value of the VM operating in client mode, using the Parallel Avenge +Serial Old (PS MarkSweep) collector to reclaim memory.
  5. -xx :+UserParallelOldGC: recycle memory using the Parallel+Scavenge+Parallel Old collector.
  6. -xx :SurvivorRatio :SurvivorRatio of Eden and Survivior capacity in Cenozoic era. The default value is 8, indicating Eden: Survicor = 8:1
  7. – XX: PretenureSizeThreshold: the size of the object directly promoted the old s, set this parameter, is greater than the object of this parameter will directly in the old s distribution.
  8. -xx :MaxTenuringThreshold: The age of objects promoted to the old age. After each Minor GC, the age of each object is +1. When this value is exceeded, the old age is entered.
  9. -xx :UseAdaptiveSizePolicy: dynamically adjusts the size of each area in the Java heap and the year to enter the old age
  10. – XX: HandlePromotionFailure: whether to allow assignment guarantees failure, namely the remaining space is not enough to cope with the new generation of the old s Eden and survivor area all objects survive extremes.
  11. -xx :ParallelGCThreads: Sets the number of threads that can be recycled during parallel GC.
  12. -xx :GCTimeRatio: ratio of GC time to total time. The default value is 99, that is, 1% GC time is allowed. Only valid when using the Parallel Scavenge collector.
  13. -xx :MaxGCPauseMillis: specifies the maximum pause time of the target. Only valid when using the Parallel Scavenge collector
  14. – XX: CMSInitiatingOccupancyFraction: set the CMS collector after the old s space is how much triggers garbage collection. The default value is 68% and only takes effect when the CMS collector is used
  15. – XX: UseCMSCompactAtFullCollection: set the CMS collector after complete garbage collection is to conduct a memory defragmentation. Only valid when using the CMS collector
  16. – XX: CMSFullGCsBeforeCompaction: set the CMS collector in start again after several times of garbage collection memory defragmentation. Only valid when using the CMS collector

Now there is a new generation of collector: ZGC, which is a low-latency garbage collector released in JDK 11. For details, please refer to the exploration and practice of Meituan’s new generation of garbage collector ZGC

Summary -GC collector selection:

Unless the application has very strict pause time requirements, run the application first and allow the VM to select the collector (if not specifically required). Just use the default GC provided by the VM). If necessary, adjust the heap size to improve performance. If performance still does not meet your goals, use the following criteria as a starting point for selecting a collector:

  • If your application has a small data set (about 100 MB at most), select the serial collector with the option -xx :+UseSerialGC.
  • If the application will run on a single processor and there is no pause time requirement, select the serial collector with the option -xx :+UseSerialGC.
  • If peak application performance is the top priority and no pause times are required or pauses of a second or more are acceptable, let the VM select the collector or select the parallel collector using -xx :+UseParallelGC.
  • If response time is more important than overall throughput, and the garbage collection pause time must be kept within about a second, choose to have -xx :+UseG1GC. Note that CMS in JDK9 has been Deprecated and will not be available! Remove this option.
  • If you are using JDK8 and the heap memory is up to 16GB, it is recommended to use the G1 collector to control the timing of each garbage collection.
  • If the response time is of high priority, or the heap used is very large, use -xx :UseZGC to select a fully concurrent collector. (It should be noted that ZGC can be started in JDK11, but at this point ZGC is experimental. In JDK15, the experimental tag is unmarked, and the default GC in JDK15 is G1.)

These guidelines provide only a starting point for selecting a collector, because performance depends on the size of the heap, the amount of real-time data maintained by the application, and the number and speed of processors available. If the recommended collector does not achieve the desired performance, the heap and Cenozoic sizes are first attempted to achieve the desired goal. If performance is still insufficient, try another collector

General principles: Reduce STOP THE WORD time, use concurrent collectors (such as CMS+ParNew, G1) to reduce pause times, speed response times, and use parallel collectors to increase overall throughput on multiprocessor hardware.

The original address


I am Mr. Ji, with the output force input and continue to learn, continue to share the series of technical articles, as well as the whole network worth collecting good articles, welcome to pay attention to the public number, do a continuous growth of technical people.

Personal website

The JVM virtual machine series historical articles

1. Virtual Machine series: how to divide JVM runtime heap memory;

2. Virtual Machine series: Garbage collection algorithm in JVM;

3. Virtual Machine Series: JVM runtime data area;

4. Virtual Machine series: creation of objects in JVM, memory layout and access location;