[jvm series-10] In-depth understanding of the types of jvm garbage collectors and internal execution principles

JVM series overall column


content link address
[1] Getting to know virtual machines and java virtual machines https://blog.csdn.net/zhenghuishengq/article/details/129544460
[2] The class loading subsystem of jvm and the basic use of jclasslib https://blog.csdn.net/zhenghuishengq/article/details/129610963
[3] The virtual machine stack, program counter, and local method stack of the private area at runtime https://blog.csdn.net/zhenghuishengq/article/details/129684076
[4] Heap and escape analysis of the shared area of ​​the data area at runtime https://blog.csdn.net/zhenghuishengq/article/details/129796509
[5] The method area and constant pool of the runtime data area shared area https://blog.csdn.net/zhenghuishengq/article/details/129958466
[6] Object instantiation, memory layout and access positioning https://blog.csdn.net/zhenghuishengq/article/details/130057210
[7] Execution engine, interpreter, JIT instant compiler https://blog.csdn.net/zhenghuishengq/article/details/130088553
[8] Proficient in the underlying mechanism of String https://blog.csdn.net/zhenghuishengq/article/details/130154453
[9] The underlying principles and algorithms of garbage collection and the basic use of JProfiler https://blog.csdn.net/zhenghuishengq/article/details/130261481
[10] Types of garbage collectors and internal execution principles https://blog.csdn.net/zhenghuishengq/article/details/130261481

One, the garbage collector in the jvm

1. Overview of Garbage Collector

In the "Java Virtual Machine Specification", there are no clear regulations on the garbage collector, so the garbage collector can be implemented by any manufacturer and different versions of the JVM. Therefore, analyzing this garbage collector from different angles can divide GC garbage collectors into different types
insert image description here

If it can be classified according to the number of threads executing garbage threads, it can be divided into serial garbage collectors and parallel garbage collectors ; it can also be distinguished according to the working mode, which can be divided into concurrent garbage collectors and exclusive garbage collectors ; It can be divided into compressed garbage collector and non-compressed garbage collector according to the processing method of fragments; it can also be divided into young generation collector and old generation collector according to the working memory area .

When faced with the GC evaluation of a garbage collector, it is mainly evaluated from the following aspects

  • Throughput: The ratio of the time spent running user code to the total running time (total running time = program running time + garbage collection time)
  • Garbage Collection Overhead: The ratio of garbage collection to total runtime
  • Pause time: When performing garbage collection, the time when the program's worker thread is suspended (stw)
  • Collection Frequency: How often collection operations occur relative to the execution of the application
  • Memory occupation: the memory size occupied by the Java heap area.
  • Cycle: the time an object takes from its birth to its recycling

Among these indicators, the three indicators of throughput, pause time and memory usage are the most important. Because these three can only satisfy two of them at a time, the throughput and this pause time can only be selected from one of the two.

1.1, throughput and pause time

As mentioned above, this throughput refers to the ratio of the time the CPU spends running user code to the total CPU consumption time, that is, the time spent running user code/the time spent running user code+garbage collection time, such as the total running time of the virtual machine 100 minutes, garbage collection took 1 minute, and running user code took 99 minutes, then its throughput is 99 / 100 = 99%.

If you focus on throughput, you don’t need to consider the size of the pause time. As shown in the figure below, within 6s, although the above pause time is relatively long, the proportion of the pause time is relatively small, so it can be considered that more emphasis is placed on throughput. , it can also be considered that the garbage collector that focuses on throughput does not need to consider the pause duration of this stw

insert image description here

The garbage collector that focuses on low latency has a lower throughput than the above one, but its latency is small, so the time for each STW will be shorter, and the required space can also be smaller. Therefore, it can also be proved that the above throughput and this pause time are contradictory.

The advantage of high throughput is that it can make the application feel that only the application thread is doing productive work. From an intuitive point of view, the higher the throughput, the faster the program runs; the advantage of low latency is that from the perspective of the entire process, if it is Because an application is suspended for a period of time is always unfriendly, because a stw pause of 200ms may also seriously affect the user experience, so it is necessary to have a lower pause. Therefore, if it is an interactive application, then lower latency is the priority. If Juggernaut goes down with a q, you get stuck for a few seconds and then lose blood, then I have to hate this game to death.

If throughput is preferred, for example, the execution frequency of recycling will be reduced, but at the same time, the time of STW will be longer; if low latency is preferred, the frequency of recycling will increase, which will lead to the reduction of memory in the new generation and the reduction of program throughput decline

Therefore, the standard of the current garbage collector is: in the case of maximum throughput priority, and then reduce the pause time

1.2, types and overview of garbage collectors

Next, through the history of this garbage collector, to explain in detail the types of garbage collectors.

  • In 1999, Serial GC was born, it is the first GC, running in serial mode
  • In 2002, Parallel GC and CMS GC were released at the same time, running in parallel , and the default GC started from JDK6
  • In 2017, G1 became the default garbage collector in JDK9, replacing CMS
  • In 2018, JDK11 was released and introduced ZGC, a scalable low-latency garbage collector
  • In 2019, JDK12 was released, enhanced this G1, and automatically returned unused heap memory to the operating system
  • In 2019, JDK13 was released, which enhanced ZGC and automatically returned unused heap memory to the operating system
  • In 2020, JDK14 was released, CMS was deleted, and CMS became history

The collocation of the garbage collector is as follows, the ParNew GC is used with this CMS, the Parallel Scavenge GC is used with the Parallel Old GC, and the Serial GC is used with the Serial Old GC. Among these three combinations, the former is used to recycle the new generation, and the latter is used to recycle the garbage of the old generation. For example, the blue part below is used to recycle the new generation, and the orange part is used to recycle the old generation. The G1 garbage collector introduced later can recycle both the new generation and the old generation.

insert image description here

Through the garbage collector above, we know that there are 7 classic garbage collectors

  • Serial Garbage Collector: Serial, Serial Old
  • Parallel collectors: ParNew, Parallel Scavenge, Parallel Old
  • Concurrent collectors: CMS, G1

In JDK8, the default is mainly the combination of Parallel Scavenge + Parallel Old, or the combination of CMS + ParNew. If it is on the C side and it can be confirmed that it is running in a single-threaded environment, then you can also choose to use the combination of Serial + Serial Old. The performance of Parallel and ParNew is comparable, but due to the incompatibility of the framework, only this ParNew can be combined with this CMS, and this Parallel cannot be combined with this CMS.

The command to view the current default garbage collector is as follows:

-XX:+PrintCommandLineFlags

Or view it in the form of command

//查看所有进程
jps
//通过进程号查看对应的信息
jinfo -flag UseParallel 进程号

2. Serial Garbage Collector

This collector is the most basic and oldest serial garbage collector.

The following figure describes in detail the execution process of the entire garbage collection in Serial. In the new generation, the copy algorithm is used, such as the typical s0 area and s1 area, and the serial recovery and stw mechanism are used to perform memory recovery; in the old generation, the mark collation method is used, and the It is the Serial Old collector used in conjunction with Serial, and it also uses the serial recycling and stw mechanism. The stw mechanism is generally triggered when a SafePoint is safe, and at this time all user threads are suspended to execute garbage collection threads

insert image description here

Serial Old has two main uses, one is to be used in conjunction with the new generation of Serial, and the other is to be used as a substitute for the old generation CMS.

Although Serial is a collector in serial mode, it also has its own advantages compared to other collectors. For a single CPU, the Serial collector does not have the overhead of thread interaction, and concentrates on garbage collection, so it can obtain the highest single-thread efficiency. For example, in user desktop application scenarios, the available memory is generally not large, and garbage collection can be completed in a relatively short period of time. As long as the frequency of garbage collection is not too frequent, then this serial garbage collector can be selected first.

In the jvm virtual machine, the parameters for using the Serial collector are as follows

-XX:+UseSerialGC

You can also view it from the command line

//查看所有进程
jps
//通过进程号查看对应的信息
jinfo -flag UseSerialGC 进程号

The disadvantage is that it can only be limited to running in a single-core CPU, which is difficult to meet the needs of contemporary developers.

3. ParNew Garbage Collector

Serial is a single-threaded garbage collector, while ParNew is a multi-threaded version of Serial. That is to say, ParNew is essentially the same as the Serial collector except that it can adopt the feature of parallel recycling .

The ParNew collector also adopts the parallel mode in the new generation, the replication algorithm, and uses the STW mechanism, and in many Server modes, it is their default garbage collector; but in the old generation, it can be used with this concurrency CMS in this mode or Serial Old in this serial mode use

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-K0Za7NGh-1682412834185)(img/1682307386705.png)]

For the new generation, the frequency of GC triggered by it will be higher, and the number of recycling times will be more frequent, so it is executed in parallel, and a space-for-time copy algorithm is used to improve efficiency; for the old generation, the number of GC It will be lower, and the number of recycling is less, so you can use the serial method and use a smoother marking method to save resources.

In the jvm virtual machine, the parameters for using the ParNew collector are as follows

-XX:+UseParNewGC

You can also view it from the command line

//查看所有进程
jps
//通过进程号查看对应的信息
jinfo -flag UseParNewGC 进程号

4. Parallel garbage collector (high throughput)

In the HotSpot virtual machine, Parallel has the same characteristics as ParNew in the new generation. They both use parallel recycling, an efficient copy algorithm that trades space for time, and the corresponding STW mechanism. The difference is that ParNew only has parallel On this basis, Parallel adds a controllable throughput feature, also known as a throughput-first garbage collector. High throughput can make efficient use of CPU time and complete computing tasks as quickly as possible, such as batch processing and order processing.

Used in conjunction with Parallel Old in the old generation , the algorithm used by Parallel Old is the mark sorting method, but it is also based on the parallel recycling and stw mechanism.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bM5FwmTB-1682412834186)(img/1682317043639.png)]

In throughput priority scenarios, the combination of Parallel and Parallel Old collectors can be given priority, and this collector is also the default collector in Java8.

In the jvm virtual machine, the parameters for using the Parallel collector are as follows

-XX:+UseParallelGC          //手动指定新生代
-XX:+UseParallelOldGC       //手动指定老年代
-XX:ParallelGCThreads       //设置新生代并行收集器的数量,大小最好设置和cpu数量相等
-XX:MaxGCPauseMillis        //设置垃圾回收器最大的停顿时间
-XX:GCTimeRatio             //垃圾收集时间占总时间的比例
-XX:+UseAdaptiveSizePolicy  //手动指定老年代

You can also view it from the command line

//查看所有进程
jps
//通过进程号查看对应的信息
jinfo -flag UseParallelGC  进程号

5. CMS Garbage Collector (Low Latency)

CMS: Concurrent-Mark-Sweep , the first truly concurrent collector, for the first time realized that the garbage collection thread and the user thread work at the same time. The focus of the CMS collector is to shorten the pause time of user programs during garbage collection as much as possible . That is to say, the shorter the pause time, the more suitable for programs that interact with users. Good response speed can improve user experience. The algorithm used for this garbage collection is mark-and-sweep .

5.1, CMS processor workflow (emphasis)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-NnmWDDVZ-1682412834186)(img/1682321410352.png)]

As can be seen from the above figure, the workflow in CMS is mainly divided into four stages, namely initial marking, concurrent marking, re-marking and concurrent cleaning

Initial marking : In this stage, the working thread of the program will be temporarily suspended due to the stw mechanism. The task of this stage is mainly to mark the objects that GC Root can associate with . Once the marking is completed, the previous state will be restored. Since the directly associated objects are relatively small, the speed here is very fast.

Concurrent marking : The process of traversing the entire object starting from the directly associated object of GC Roots is a long process, but it does not need to pause the user thread, allowing the user thread and the garbage collection thread to work at the same time.

Re-marking : Since the worker thread and the garbage collection thread work at the same time during the concurrent marking phase, it is inevitable that new garbage will appear or old garbage will be resurrected (finalize), so a re-marking is required. At this time, STW will be triggered, which takes a little longer, but far shorter than the concurrent marking phase.

Concurrent cleanup : This stage is to actually clean up the dead objects judged by the marking stage and release the memory space

Although the CMS collector uses concurrent recycling, its initialization mark and mark again will still trigger STW, but the pause time will not be too long. Since the most time-consuming concurrent marking and concurrent clearing are executed concurrently, user threads do not need to be paused, so that the overall garbage collection is low-latency .

CMS is mainly for users to recycle objects in the old age, and user threads and garbage collection threads work at the same time. If the garbage collection is triggered when the memory is full, then the user thread will not be able to run, so it cannot be started when the memory is full. To trigger garbage collection, you need to set a threshold, and when the threshold is reached, the garbage collection will be triggered.

5.2, The reason why CMS adopts mark-and-sweep algorithm

Among the current three garbage algorithms, the new generation generally uses the copy algorithm, and the old generation generally uses the mark-and-sweep method or the mark-and-sweep method. Since mark-and-clearing will generate a large number of garbage fragments, the old generation generally prefers to use the mark-and-sweep method. Why is mark clearing used here?

Since garbage collection uses a mark-and-clear algorithm, this fragmentation problem may occur. Therefore, when allocating memory, only free lists can be used instead of pointer collisions. The main reason is that the user thread is also working during concurrent cleanup. Even if the mark collation method can solve this fragmentation problem, the mark collation method will involve the movement of the object. While the user thread is working, the address of the object must not changed, so only the mark removal algorithm can be selected here, not the mark collation algorithm. The user thread is working, and if you change the address of others, they are not allowed to sue you.

If you must choose the mark collation method, then you need to stop the user thread with stw. Here, for low latency, it is obviously impossible to have stw in the concurrent cleanup phase.

5.3, the advantages and disadvantages of the CMS recycler and the parameters that can be set

CMS advantages : achieve concurrent collection; low latency

Disadvantages of CMS : The mark-and-clear method used will generate garbage fragments; it is sensitive to CPU resources; new garbage may appear in concurrent marking, so it cannot handle this kind of floating garbage

In the jvm virtual machine, the parameters for using the CMS collector are as follows

-XX:+UseConcMarkSweepGC             //手动指定使用CMS收集器
-XX:CMSInitiatingOccupanyFraction   //设置堆内存使用率阈值,达到该阈值时触发垃圾回收
-XX:+UseCMSCompactAtFullColection   //指定FULL GC之后对内存空间进行整理
-XX:CMSFullGcsBeforeCompaction      //设置多少次FULL GC之后对内存空间进行整理
-XX:ParallelCMSThreads              //设置CMS线程的数量

Therefore, through the advantages and disadvantages of these garbage collectors, we can see that the selection strategy of the garbage collector is as follows: want to minimize the use of memory and parallel overhead------Serial, maximize the throughput of the application------ Parallel, minimize interruption or pause time ------ CMS .

In JDK8, CMS is used with ParNew to become the default garbage collector; in the JDK14 version, CMS is directly removed and no longer used, and CMS becomes history.

6. G1 Garbage Disposer (Key Point)

6.1, G1 Garbage Collector Overview

Starting from JDK9, the garbage collector used is the G1 collector, which is called a regionalized generational collector . Although there are already high-throughput Parallel and low-latency CMS, as the business becomes larger, more complex, and more and more users, these two processors obviously cannot meet the actual needs, and with the increase of memory The continuous expansion and the continuous increase in the number of processors, in order to be compatible with high throughput and low latency, both can meet the needs, so G1 was born. The official goal set for G1 is to increase the throughput as much as possible while the delay is controllable.

G1 is a parallel collector that divides the heap memory into many irrelevant areas, such as Eden, s0, s1, old generation, etc. Actively track each region, calculate the value of its garbage accumulation in each region, and then maintain a priority list in the background. Each time, according to the allowed collection time, the Region with the highest value will be recovered first. That is to say, regardless of the total number of objects in the region, but which region has more garbage, which region region will be recycled first, so the origin of the name of G1 -----Garbage First (garbage first)

6.2, the characteristics of G1 collector

Parallelism and concurrency : G1 has parallelism, and multiple GCs can work at the same time during the recycling period, and STW will be triggered at this time; it also has concurrency, can be executed alternately with the application, and STW will not be triggered, and there will be no complete GC during the entire stage. blocking situation.

Generational collection : G1 is still a generational collector, and it still distinguishes between the new generation and the old generation. The difference is that the objects in the heap do not require continuous space, nor do they need to fix their size and number. As shown in the figure below, if all objects in a certain Eden area are cleared, then the object stored in that address next time may not necessarily be an object in the Eden area, but may be an object in the s area or the old area. In each region, only one type of object is allowed.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-5ZlgvA02-1682412834187)(img/1682389504407.png)]

Space integration : CMS uses a mark-and-sweep algorithm, which will cause memory fragmentation. G1 divides the memory into small regions, and the recovery of memory is also based on the region. It uses a copy algorithm internally. The whole can be regarded as a marking method to solve the problem of memory fragmentation and large objects. A GC will not be triggered early because it cannot find a continuous memory space.

Predictable pause time model : Both G1 and CMS were born for low latency. In addition to pursuing low latency, G1 can also establish a predictable pause time model, allowing users to clearly know that in a length of M milliseconds During the time judgment, the garbage collection must not exceed N seconds

6.3, parameter setting of G1 recycler

The parameters that can be set are as follows

-XX:+UseG1Gc                    	//手动指定使用G1收集器
-XX:G1HeapRegionSize           		//设置region大小,值为2的幂
-XX:MaxGcPauseMillis                //设置最大GC停顿指标
-XX:ParallelGCThread                //设置stw工作线程的值
-XX:ConcGCThreads                   //设置并发标记的线程数
-XX:InitiatingHeapOccupancyPercent  //设置触发并发GC周期的Java堆占用阈值

The tuning principle of G1 is to simplify the performance tuning of the JVM. Developers only need three simple steps to complete the tuning: enable the G1 garbage collector, set the maximum heap memory, and set the maximum pause time

6.4,Region

In the G1 collector, it divides the entire Java heap into 2048 Region blocks of independent size. The size of each region block depends on the actual size, and the value of the size is a power of 2, which can be set by -XX:G1HeapRegionSize. In the region, there is no need for logical continuity between the young generation and the old generation.

As shown in the figure below, a region can only belong to one role, E means the eden area, S means the Survivor area, O means the old area, and the blank part means the unused area. At the same time, in G1, a Humongous memory area is added, which is mainly used to store large objects. If there are more than 1.5 regions, the object will be stored in the Humongous memory area. If one H area cannot be stored, then a continuous H will be searched. zone storage

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-dxld8ypn-1682412834187)(img/1682389504407.png)]

6.5, the process of garbage collection by G1 collector

The G1 garbage collector is different from the above garbage collectors. The above collectors are mainly for garbage collection in one of the new generation or the old generation, while G1 is to perform garbage collection on the new generation. The old generation is also garbage collected. G1's garbage collector mainly includes three links: young generation GC, old generation concurrent marking process, and round collection .

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-E543SGGl-1682412834188)(img/1682393777571.png)]

  • When the Eden area of ​​the new generation is exhausted, the garbage collection process of the young generation will be triggered: The new generation collection phase of G1 is a parallel and exclusive garbage collector. During the recovery period of the young generation, G1 GC suspends all application threads and starts multi-threading to perform new generation recovery. Then move the surviving objects from the new generation to the s area or the old generation.
  • When the heap memory reaches 45%, the concurrent marking process of the old generation will be triggered
  • After the marking is completed, the process of mixed collection begins: For a mixed collection period, G1 GC moves objects from the old generation to free areas, and these free areas become part of the old area. The old generation collector does not need to recycle the entire old generation, but only needs to scan a part of the old generation area at a time.

6.5, Memory sets and write barriers

In each region, there must be mutual references between regions, so in the region, this Remember Set is introduced, which is called the result memory set. Each memory set records the address of the object that refers to the current object, and when writing to the memory set, it is called a write barrier. And when writing, there will be a short interruption operation, go back and judge whether the written object is in a different region from the current type of data, if not, then load the relevant reference information into Rset, and perform garbage collection , you only need to scan the contents of the memory set, and do not need to scan the entire disk.

As shown in the figure below, each Region area has a corresponding Rset memory set, such as the Region2 area, which is referenced by region1 and region3, then the relevant information of the region1 and region3 will be recorded in the rset corresponding to the region2 area. If Region2 is to be judged as garbage and recycled, the scope can be narrowed only by the value recorded in this Rset.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-1M4ubSDP-1682412834188)(img/1682408274118.png)]

6.6, G1 Garbage Collection Details

The recycling process of the new generation is as follows:

  • First scan the Root root, such as some static objects, local variables in the method, etc., as the entry of RSet,
  • Then update the RSet, the RSet can accurately reflect the references of the old generation to the objects in the memory segment,
  • Then process this RSet, and the object in Eden pointed to by the old generation is the surviving object.
  • Then copy the object, copy the object in the eden area to the s area or the old area, and finally process the reference, such as some strong, soft, weak, phantom references, etc.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-PUQiRiIP-1682412834189)(img/1682409311073.png)]

The process of concurrent marking is as follows : (this stage is very similar to the CMS stage)

  • First is the initial marking stage, marking objects directly reachable from the root node will trigger stw;
  • Then scan from the root area, G1 GC scans the directly reachable old generation objects in the s area, and marks these objects;
  • The second is concurrent marking, which performs concurrent marking in the entire heap. If garbage is found in this phase, the area will be recycled immediately;
  • Next is the process of marking again, correcting the result of the last marking, this project requires stw;
  • The next step is exclusive cleaning, calculating the surviving objects in each area and the recovery ratio of GC, which will trigger STW;
  • Finally, the concurrent cleanup phase identifies and cleans up completely free regions.

The mixed recovery process is as follows:

  • Using the copy algorithm, the garbage of the new generation and the garbage of the old generation are mixed and collected. Only a part of the old generation is used, not all of the old generation. The mixed recovery algorithm is exactly the same as the recovery algorithm in the new generation, except that the recovery collection has more memory segments in the old generation.

Therefore, in the G1 collector, it is necessary to avoid explicitly setting the size of the new generation with related options such as -Xmn or -XX:NewRatio, because the fixed new generation will cover the goal of the pause time, and the heap pause time should not be too harsh

7. Summary of classic garbage collectors

Through the above detailed description of various garbage collectors, the characteristics of each collector are organized as follows
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-EKAy8hCx-1682412834189)(img/1682411248279.png)]

How does the garbage collector choose

1. Prioritize the size adjustment of the heap and let the server choose by itself.
2. If the memory is less than 100M, use the serial collector.
3. If it is a single core and there is no pause time requirement, choose serial or JVM itself.
4. If the pause time is allowed For more than 1 second, choose parallel or JVM to choose
5. If the response time is the most important and cannot exceed 1 second, use concurrent collector
6. Parallel can be used below 4G, ParNew+CMS can be used for 4-8G, and G1 can be used above 8G , use ZGC for hundreds of G

8. Common parameters of GC logs

-XX:+PrintGC                      //输出GC日志类
-XX:+PrintGCDetails               //输出Gc的详细信息
-XX:+PrintGCTimeStamps            //输出GC的时间戳
-XX:+PrintGCDateStamps            //输出Gc的时间戳
-XX:+PrintHeapAtGC                //在GC前后打印出堆信息
-Xloggc:../logs/gc.log            //设置日志的输出路径

If you reprint, please attach the reprint link address: https://blog.csdn.net/zhenghuishengq/article/details/130369011

Guess you like

Origin blog.csdn.net/zhenghuishengq/article/details/130369011