JVM garbage collector (2)

Table of contents

1、Serial

2、Par New

3、Parallel Scavenge

4、Serial Old

5、Parallel Old

6、CMS

1. Why do you need to "stop the world" twice?

2. Problems caused by CMS concurrency

3. CMS triggering timing

4. Defects of CMS

5. Why does CMS use a clearing algorithm?

7、G1

1、Region

2. The significance of designing Region

3. Three modes of G1

4. Operation process of Mixed GC

5、Card Table

6. Three-color marking method

7、STAB

8. Features of G1

8. JDK default garbage collector


The specification does not stipulate how to implement the garbage collector, so different virtual machines provide various collectors for users to combine and use according to their needs.

Various collectors for HotSpot:

There is a connection between two collectors, indicating that they can be used together. The reason why so many combinations are provided is because each collector has its own advantages and disadvantages, and there is no perfect collector yet.

1、Serial

This is the most basic collector, used for the new generation, and its name means "serial".

This is a single-threaded collector, which means it uses only one thread for garbage collection.

标记-复制algorithm.

Its principle and process are relatively simple, but since the action of stopping the thread is initiated by the virtual machine, the user thread is unknown and it is not very good to be stopped suddenly.

A major goal of subsequent garbage collectors is to shorten the pause time of user threads, but there is always no way to avoid pauses.

2、Par New

This is a multi-threaded parallel version of Serial. The number of collection threads enabled by default is the same as the number of CPUs. Multiple threads can be used at the same time for parallel garbage collection.

Apart from Serial, only ParNew works with CMS. CMS is of epoch-making significance, and later ParNew can be regarded as a dedicated new generation collector that was merged into CMS.

标记-复制algorithm.

3、Parallel Scavenge

It is also aimed at the new generation and supports multi-threaded parallel collection, which is characterized by different concerns.

  • The focus of collectors such as CMS is to shorten the pause time of user threads during garbage collection as much as possible

  • The focus of Parallel Scavenge is 保证一个可控制的吞吐量to ensure user experience.

    (Throughput here refers to the proportion of user code execution time per unit time (running user code + garbage collection))

Due to its focus on throughput, Parallel Scavenge is also called a "throughput-first collector".        

Suitable for scenarios where throughput is important or where processor resources are scarce.

标记-复制algorithm.

4、Serial Old

This is the old generation version of Serial, which is also single-threaded and uses 标记-整理algorithms.

5、Parallel Old

This is the old generation version of Parallel Scavenge, which supports multi-threaded concurrent collection and uses 标记-整理algorithms.

6、CMS

CMS (Concurrent Mark Sweep) is a 最短回收停顿时间collector that aims to reach " ". Suitable for servers that care about response speed.

It is more complex than the general mark-sweep algorithm and is divided into the following 4 stages:

  • Initial mark: Stop The World, marking objects that GC Roots can directly associate with, which is faster.
  • Concurrent marking: The GC Roots tracking marking process can be executed concurrently with the user thread without pausing the user thread.
  • 重新标记: Stop The World, re-judge the viability of objects generated during marking, correct the marking of these objects, and the execution time is shorter than concurrent marking.
  • Concurrent clearing: clearing objects can be executed concurrently with user threads.

CMS是清理老年代的

1. What to do at each stage

initial mark

Working mode: single-threaded before JDK7, multi-threaded after JDK8

Purpose: Mark surviving objects

Includes two parts:

  • Mark objects in the old generation that GC Roots can directly relate to
  • Mark objects that are still alive in the new generation and contain references to the old generation, that is, additional cross-generation references from the new generation to the old generation are taken into account.

This stage will stw. In order to shorten the pause time, you can enable initial mark parallelization, -XX:+CMSParallelInitialMarkEnabled, and increase the number of concurrent mark threads, but do not exceed the number of CPU cores.

concurrent marking

Purpose: Follow the surviving objects marked in the initial marking phase to find all surviving objects.

Because it is executed concurrently with the user thread, these actions may occur during the period:

  • New generation objects are promoted to the old generation
  • Allocate large objects directly in the old generation
  • The reference relationship between objects in the old generation changes

For these objects, it is necessary to re-mark the current latest status, otherwise the surviving objects may be missed.

In order to improve the efficiency of re-marking and avoid re-scanning the entire old generation, after discovering the above behavior at this stage, the card where the object is located will be marked as a dirty card. Subsequently, only all dirty cards need to be scanned for processing.

Therefore, after the concurrent marking phase is completed, not all surviving objects in the old generation will be marked, and some will be recorded as dirty cards, waiting for subsequent processing.

Pre-cleaning phase

Purpose: Scan all dirty cards, check the reference relationships of all objects in the dirty cards, and mark surviving objects

Terminable preprocessing

Purpose: At this stage, try to carry out the re-marking phase, because the re-marking phase will stw, which can reduce some pause time.

The maximum duration of this phase is 5 seconds, because it expects a young gc to occur within these 5 seconds to clean up the new generation, thereby reducing the time for scanning cross-generation references in the next phase.

relabel

Purpose: Complete the marking of all living objects in the entire old generation, stw

Use the incremental update algorithm in three-color marking for re-marking

Tuning:

Although the purpose of this stage is to mark the old generation, it needs to scan the entire heap because there may be cross-generation references to the old generation in the new generation.

For high efficiency, you can add the parameter -XX:+CMSScavengeBeforeRemark to perform a young gc before remarking.

In this way, only the smaller new generation needs to be scanned, greatly improving efficiency.

concurrent cleanup

1. Why do you need to "stop the world" twice?

During the "initial marking" phase, CMS will quickly scan for objects that can be directly associated with GC Roots, and then unpause.

It is then executed concurrently with the user thread to perform object reachability analysis.

However, during the execution of the user thread, the reference relationship is likely to change, so the world is stopped again and the marks of the objects whose references have changed are modified.

For example, if an object is judged to be "dead" for the first time, and then the user thread re-establishes a reference relationship with it, then it will be modified to "alive" for the second time.

Note: Objects that are not marked in the "Initial Marking" phase will not be marked as garbage objects in the "Remarking" phase.

The pause time in this phase is generally slightly longer than the initial phase, but much shorter than the concurrent marking time.

Due to the longest "concurrent marking" and "concurrent cleaning" processes in the entire process, the collector thread can work together with the user thread. So in general, the memory recycling process of the CMS collector is executed concurrently with the user thread.

Advantages: concurrent collection, low pauses

2. Problems caused by CMS concurrency

In the concurrent phase, although it will not cause the user thread to pause, it does occupy a part of the CPU's running resources, causing the application to slow down and reducing the total throughput.

The default number of recycling threads started by CMS is (number of processor cores + 3)/4. When there are less than 4 processor cores, CMS has a greater impact on user programs.

3. CMS triggering timing

The CMS collector cannot wait until the old generation is almost full before collecting like other collectors. This is because CMS需要预留足够的内存给用户线程使用.

The default policy is to start recycling after the old generation uses 68% of the space.

  • If the policy threshold is set lower, garbage collection will occur more frequently, affecting performance.
  • If the policy threshold is set too high, and if the memory reserved by CMS cannot meet the needs of the program to allocate new objects, the JVM will initiate an emergency plan: freezing the execution of user threads, temporarily enabling Serial Old to re-garbage the old generation. This makes the pause time very long.

4. Defects of CMS

  • The CMS collector is very sensitive to processor resources, because although it will not stop the user thread, it will compete with the user thread for processor resources, causing the user thread to execute slower.
  • CMS cannot handle " 浮动垃圾", which may result in the GC not generating enough space and having to trigger a Full GC.
  • CMS is based on 标记-清除algorithms and will generate a large number of them 空间碎片. If object allocation is affected, a Full GC must be triggered to organize memory.
  • If the user thread suddenly generates a large amount of garbage while the CMS is running, the JVM will urgently pause the user thread and use Serial Old to re-garbage the old generation.

What is floating garbage

The first time the "initial mark" is judged, the object is not garbage. But during the second judgment of "remarking", this object becomes garbage, then this garbage collection cannot process it, and we can only wait until the next GC to have the opportunity to recycle it. This kind of object is floating garbage.

5. Why does CMS use a clearing algorithm?

Because CMS considers minimizing the pause time of user threads caused by garbage collection, but the workload cannot be reduced, then consider allowing user threads and garbage collection to execute concurrently at certain stages.

Because of this, when the user thread is executing normally, CMS cannot modify the address of any object without authorization, otherwise the user thread will not be able to locate the object.

Both the copy algorithm and the collation algorithm need to change the memory address of the object, so they are not suitable.

7、G1

G1 is a high-performance garbage collector for servers, mainly targeting machines with multi-core processors and large memory.

In JDK 1.9, G1 was used as the default garbage collector, and CMS was marked as "deprecated".

While G1 meets GC pause time requirements with a very high probability, it also has high throughput performance characteristics.

1、Region

The layout of G1 heap memory is different from other collectors.

G1不再进行固定大小以及固定数量的分代区域划分,而是把连续的Java堆划分成多个大小相等的独立区域(Region)

The size of the region is consistent, and the value is a power of 2 between 1M and 32M bytes. The JVM will try to divide it into about 2048 regions.

Each Region can play the role of Eden, Survivor or old generation space according to needs.

G1 can use different collection strategies for Regions that play different roles, so that both newly created objects and older objects can achieve good collection results.

There is also a special type of Humongous area in Region, which is dedicated to storing large objects.

If an object exceeds 50% of the Region capacity, it will be stored in N consecutive Humongous Regions, and most behaviors of G1 will treat it as part of the old generation.

Since the heap memory is fragmented, a free list needs to be maintained to record all available regions.

2. The significance of designing Region

The concepts of new generation and old generation still exist in G1, but they are not continuous, but a dynamic collection of a series of Regions.

The basis for G1 to establish a predictable pause time model is to choose Region as the smallest unit for a single recycling.

The specific method is that it maintains a priority list based on the "value" of garbage accumulation in each Region. Value includes two aspects:

  • The amount of space that can be obtained after recycling
  • Time required to recycle

Each time the garbage is collected, 根据用户指定的允许停顿时间,优先回收那些价值大的Region,保证了有限时间内的较高效率.

This is equivalent to a change in the idea of ​​garbage collection:

  • In the past, priority was given to recycling the new generation, because the new generation can often obtain larger memory. If the new generation is not enough, the entire heap will be collected.
  • Now, priority is given to recycling memory with high recycling value. This is a proactive behavior, so the efficiency is much higher than before because it is more targeted.

3. Three modes of G1

Young GC

When all eden regions are exhausted and cannot apply for memory, a young gc will be triggered. The user thread will be suspended and multiple garbage collection threads will be initiated.

Surviving objects will be copied to the survivor region or promoted to the old region. The cleaned region will be placed in the free list, waiting to be used next time

Mixed GC

All previous garbage collectors either targeted the new generation, the old generation, or the entire heap for garbage collection.

When more and more objects are promoted to the old region and reach the set threshold, a Mixed GC will be triggered to recycle high-value objects.

Full GC

If the object memory allocation speed is too fast and the mixed gc has no time to recycle, causing the old generation to be filled, a full gc will be triggered.

The full gc algorithm of G1 is a serial old gc executed by a single thread, which will cause the user thread to be suspended for a long time, so Full GC needs to be avoided.

4. Operation process of Mixed GC

  • Initial markup:

    • stop the world, mark the objects that GC Roots can directly associate with
    • And modify the value of the TAMS pointer so that the user thread in the next stage can correctly allocate new objects in the available space.
  • Concurrent marking: Starting from GC Roots, perform reachability analysis on objects in the heap. Execute concurrently with user threads (only the concurrent marking phase can be executed concurrently with user threads)

  • 最终标记:stop the world, process STAB records left after the concurrent phase ends

  • 筛选回收

    • Pause the user thread according to the time set by the user, because it involves modification of the object address.
    • Sort the value of the Region, formulate a recycling plan based on the user's expected pause time, and form a recycling collection
    • Then copy the surviving objects of the part of the Region decided to be recycled to the empty Region, and clean up all the space of the old Region.

5、Card Table

The cross-generation reference problem encountered in traditional garbage collection also exists in G1.

Since G1 splits the entire heap into many regions, different objects in each region have mutual reference dependencies, and same-generation references also occur between different regions.

If you need to traverse all regions to achieve accurate garbage collection before recycling, the efficiency is extremely low.

G1 also adopted the idea of ​​Remember Set memory set and made a Card Table.

The card table stores the reference relationships between various Regions, so that only the relevant Regions can be scanned without scanning the entire region.

6. Three-color marking method

G1, like CMS, uses the three-color marking method in the concurrent marking phase:

Missing bid problem

The normal reference relationship is:

  • An object scan is completed as a gray node
  • Scan the member variables of the object, that is, other objects that have a reference relationship with it. After the scan is completed, the previous object becomes a black node.
  • Objects that have not yet been scanned are represented as white nodes
  • If the user thread deletes the reference between the gray node and the white node during the concurrent marking phase, the white node will not be scanned when scanning the member variables of the gray node, and it will be treated as garbage.
  • However, if the white node and the black node have a reference, then this reference cannot be detected by the existing scanning method, which will cause the problem of nodes that originally have a reference relationship being missed.

How CMS and G1 solve the problem of missing bids

There are two conditions for the problem of missing bids:

  • The black object points to the white object (pay attention to the increase in references)
  • The reference of the gray object pointing to the white object disappears (pay attention to the deletion of the reference)

Therefore, to solve the problem of missing bids, just break one of the two conditions.

  • What CMS does is:
    • During the concurrent marking process, the "incremental update" mechanism is used. If a new reference is generated, the black node will be marked gray, and will be rescanned later to obtain the latest reference relationship.
    • After concurrent marking is completed, the number of objects to be recycled will not increase.
    • After the concurrent marking is completed, a second pause is triggered, and the objects with re-established reference relationships are moved out of the recycling scope.
    • New garbage generated during concurrent marking will not be recycled.
  • What G1 does is:
    • New garbage generated during the concurrent marking phase will be recorded, and then included in the recycling scope during the final marking phase.
    • When gray->white disappears, this reference should be pushed to the GC stack to ensure that white can still be scanned by GC.

Why doesn't G1 use incremental update mechanism?

Because if the black node is marked as a gray node, it will be searched twice later, which is inefficient.

G1 saves the card table, which stores the reference relationships between regions.

7、STAB

G1 uses the original snapshot (STAB) algorithm to solve, Snapshot-At-The-Beginning.

The specific steps are:

  • Before GC starts, create an object snapshot.
  • During concurrent marking, all surviving objects in the snapshot at that time are considered alive. Newly allocated objects during the marking process will also be marked as alive objects and will not be recycled.

In order to achieve the concurrent operation of GC and user threads, it is necessary to solve the allocation of new objects during the recycling process, so G1 designs two pointers named TAMS (Top at Mark Start) for each Region area, and allocates a part of the space from the Region area. Used to record new objects during concurrent recycling. Such objects are considered alive and are not included in garbage collection.

8. Features of G1

  • Parallelism and concurrency: G1 can make full use of the hardware advantages of CPU and multi-core environments, and use multiple CPUs (CPUs or CPU cores) to shorten Stop-The-World pause time. Some other collectors originally need to pause the GC actions executed by the Java thread, but the G1 collector can still allow the Java program to continue executing in a concurrent manner.
  • Generational collection: Although G1 can manage the entire GC heap independently without the cooperation of other collectors, it still retains the concept of generational collection.
  • Spatial integration: Different from the "mark-clean" algorithm of CMS,  从整体来看是基于“标记整理”算法the collector implemented by G1 从局部(两个Region之间)上来看是基于“复制”算法is implemented, so 不会产生内存碎片.
  • Predictable pauses: This is another advantage of G1 over CMS. Reducing pause time is a common focus of G1 and CMS, but G1  除了追求低停顿外,还能建立可预测的停顿时间模型allows users to clearly specify the allowed pause time.

8. JDK default garbage collector

jdk1.7 default garbage collector: Parallel Scavenge (new generation) + Parallel Old (old generation)

jdk1.8 default garbage collector: Parallel Scavenge (new generation) + Parallel Old (old generation)

jdk1.9 default garbage collector: G1

Guess you like

Origin blog.csdn.net/m0_62609939/article/details/130659505