JVM garbage collection algorithm and garbage collector (Serial, ParNew, CMS) - JVM series (4)

1. Garbage collection algorithm

insert image description here

1. Generational collection algorithm

At present, most garbage collectors use the generational collection algorithm. This algorithm is actually an idea: according to the different life cycles of objects, the memory is divided into young generation and old generation, so that you can choose the appropriate one according to the characteristics of each age. garbage collection algorithm. For example, in the young generation, most of the objects will die every time they are collected (not referenced by the GC root). You can choose to 复制算法complete each garbage collection with only a small amount of object copying cost; while the objects in the old generation The probability of survival is relatively high, and there is no additional space to guarantee its allocation, so choose 标记清除算法or 标记整理算法perform garbage collection; it should be noted that generally speaking, the mark-clearing algorithm or mark-cleaning algorithm will be much slower than the copying algorithm!

2. Replication Algorithm

Simply put, the copy algorithm is to divide the memory into two areas of the same size. Each time one of the areas is used, when the memory area is used up, the surviving objects are copied to another memory area, and then Clean up the previously used memory area at one time, so that each memory recovery is half of the memory area. Generally speaking, space is exchanged for time, and the replication algorithm is suitable for application in the young generation (s1:s2 in the survivor area in the young generation is exactly 1:1)

  • Advantages: high garbage collection efficiency, no memory fragmentation
  • Disadvantage: take up memory space
    insert image description here

3. Mark Sweep Algorithm

The algorithm is generally divided 标记into 清除two stages: mark the surviving objects, and recycle all unmarked objects uniformly (generally choose this); or in turn, mark all objects that need to be reclaimed, and unify after the mark is completed Recycle all marked objects.

  • Advantages: Save memory space (compared to the copy algorithm), and the efficiency is higher than the mark sorting algorithm
  • Disadvantages: If there are too many marked objects, the efficiency is not high, and a large number of discontinuous memory fragments will be generated after the mark is cleared
    insert image description here

4. Markup Algorithm

The marking and sorting algorithm is a garbage collection algorithm based on the characteristics of the old generation, which is divided into 标记two 整理stages; the marking process is still the same as the marking and cleaning algorithm, but the subsequent steps are not to directly recycle the recyclable objects, but to let all surviving objects The object of the object moves to one end, and then directly cleans up the memory outside the end boundary. It is an algorithm that trades time for space

  • Advantages: save memory space (compared to the copy algorithm), avoid memory fragmentation
  • Disadvantages: Garbage collection is the least efficient (compared to copying and mark-and-sweep algorithms)
    insert image description here

5. Comparison of the characteristics of the three garbage collection algorithms:

copy algorithm mark-and-sweep algorithm Markup Algorithm
speed fastest medium slowest
space overhead Typically needs 2x the size of live objects (doesn't pile up fragments) Less (but builds up debris) Less (does not accumulate debris)
Whether to move the object yes no yes
  • Why is the copy algorithm faster than the mark-sweep and mark-compact algorithms?
    Personal understanding: In the case of a single thread, only one traversal is required for copying. It can be understood as marking and copying while updating references, that is, mark traversal. Because of one traversal, it is faster than mark clearing in the case of eternity. After copying, the order of the objects in memory is arranged according to the reference relationship, which is consistent with the order of the marks. For the mark sorting algorithm, there is an additional process of defragmenting the heap space, so it is more time-consuming.


2. Garbage collector

Combination relationship between garbage collectors

insert image description here
The garbage collection of the JVM is realized by these garbage collectors. Each garbage collector has its own characteristics. There is no perfect garbage collector. We need to choose the appropriate garbage collector according to different application scenarios. .

1. Serial collector

The Serial (serial) collector is a relatively early garbage collector. It is a single-threaded garbage collector. During the garbage collection process, only one thread is used to complete the collection work, and all other threads must be suspended during this process. The worker thread (that is, "Stop The World") will not continue to run until the garbage collection is complete.其年轻代采用复制算法,老年代采用标记整理算法

Collector setup parameters:-XX:+UseSerialGC -XX:+UseSerialOldGC

  • Advantages: simple and efficient (compared with other single-threaded collectors), the Serial collector can naturally obtain high single-threaded collection efficiency because it has no thread interaction overhead; it will not generate floating garbage
  • Disadvantages: Stop The World for a long time may bring bad user experience

insert image description here

Supplement: The Serial Old collector is an old version of the Serial collector, which is also a single-threaded collector. It has two main uses: one is used in conjunction with the Parallel Scavenge collector in JDK1.5 and earlier versions, and the other is used as a candidate for the CMS collector.

2. Parallel Scavenge collector

The Parallel collector is actually a multithreaded version of the Serial collector. The difference is that it uses multithreading for garbage collection. Other behaviors such as collection algorithms and recycling strategies are similar to the Serial collector. The default number of collection threads is the same as the number of CPU cores. The number of collection threads can also be set through parameters -XX:ParallelGCThreads; throughput (efficient use of cpu) is the focus of the Parallel collector, that is, the ratio of the time spent on running user code in the CPU to the total time consumed by the CPU.其年轻代采用复制算法,老年代采用标记整理算法。

Collector setup parameters:-XX:+UseParallelGC -XX:+UseParallelOldGC

  • Advantages: no floating garbage is generated, and the throughput of garbage collection is improved by using multithreading
  • Disadvantage: The user thread Stop The World will be suspended during the garbage collection process, which will bring a bad user experience

insert image description here
The Parallel Old collector is the old generation version of the Parallel Scavenge collector. Use 多线程and 标记整理算法. When focusing on throughput and CPU resources, the Parallel Scavenge collector and the Parallel Old collector ( JDK8默认的年轻代和老年代收集器) can be given priority.

3. ParNew collector

The ParNew collector is actually very similar to the Parallel collector, the main difference is that it can be used in conjunction with the CMS collector. 新生代采用复制算法,老年代采用标记整理算法。It is the first choice for many virtual machines running in Server mode. Except for the Serial collector, it is the only one that can work with the CMS collector.

Collector setup parameters:-XX:+UseParNewGC
insert image description here

4. CMS collector

The CMS (Concurrent Mark Sweep) collector is a collector that aims to obtain the shortest recovery pause time. It is very suitable for use in applications that focus on user experience. It is the first real concurrent collector for the HotSpot virtual machine, which enables the garbage collection thread and the user thread (basically) to work at the same time.

Collector setup parameters:-XX:+UseConcMarkSweepGC

It is not difficult to see from its name that the CMS collector is an implementation-based 标记清除算法collector, and its operation process is more complicated than the previous garbage collectors. The whole process is divided into four steps:

  • Initial marking : Suspend all application threads and record the objects directly referenced by GC Root. This stage is very fast.
  • Concurrent marking : The concurrent marking phase is the process of traversing the entire object graph from the directly associated objects of the GC Root. This process is time-consuming but does not need to suspend user threads, and can run concurrently with garbage collector threads; it is important to note that this phase Because the user thread is also running, it may cause the state of the marked object to change.
  • Re-marking : This stage is to correct the marking records (mainly missing marks) of the part of the objects whose markings have changed due to the continuous running of the user thread in the previous concurrent marking phase. The pause time of this phase is generally longer than that of the initial marking phase. Slightly longer, but shorter than the concurrent mark phase. It mainly uses the incremental update algorithm in the three-color mark for re-marking.
  • Concurrent cleanup : Enable user threads to continue running, and at the same time, the GC thread also starts to clean up unmarked areas. At this stage, if there are new objects, they will be directly marked as black without any processing (see the following three-color marking algorithm for details).
  • Concurrent reset : Reset the marked data in this GC process.

insert image description here

  • Advantages: concurrent collection, low pause (STW time will be relatively short)
  • shortcoming:
    • (1) Sensitive to CPU resources (because it runs concurrently with application threads, it will compete with services for resources);
    • (2) Unable to handle floating garbage (garbage is generated during the concurrent marking and concurrent cleaning phases, and this floating garbage can only be cleaned up after the next GC);
    • (3) The recovery algorithm it uses will 标记清除算法lead to a large amount of memory fragmentation at the end of the collection. Of course, this can be done by setting parameters -XX:+UseCMSCompactAtFullCollectionto let the jvm clean up after executing the mark clear;
    • (4) Uncertainty in the execution process, there will be garbage collection that has not been executed last time, and then gc is triggered again, especially in the concurrent marking and concurrent cleaning phases, it is easy to run while recycling the reference thread, maybe it has not been recycled yet again Trigger Full gc, which will cause " concurrent mode failure", thus entering STW to use the Serial old garbage collector to handle the recycling task.

Related core parameters of CMS:

-XX:+UseConcMarkSweepGC:Enable cms;
-XX:ConcGCThreads:the number of concurrent GC threads;
-XX:+UseCMSCompactAtFullCollection:do compaction after FullGC (reduce fragmentation);
-XX:CMSFullGCsBeforeCompaction:how many times to compress after FullGC, the default is 0, which means that it will be compressed once after each FullGC;
-XX:CMSInitiatingOccupancyFraction:FullGC will be triggered when the usage of the old generation reaches this ratio (The default is 92, which is a percentage);
-XX:+UseCMSInitiatingOccupancyOnly:only use the set recovery threshold ( -XX:CMSInitiatingOccupancyFractionset value), if not specified, JVM will only use the set value for the first time, and will automatically adjust later;
-XX:+CMSScavengeBeforeRemark:start before CMS GC A minor gc reduces the overhead of the CMS GC marking phase (the young generation will also be marked together, if a lot of garbage objects are killed in the minor gc, the marking phase will reduce some marking time), and the general CMS GC time-consuming 80% are in the marking stage;
-XX:+CMSParallellnitialMarkEnabled:it means multi-threaded execution during initial marking, shortening STW;
-XX:+CMSParallelRemarkEnabled:multi-threading execution during re-marking, shortening STW;

3. The underlying principle of garbage collection

1. Three-color marking

In the process of concurrent marking, because the application thread continues to run during the marking period, the references between objects may change, and the situation of multi-marking and missing marking may occur. The problem of missing marking is mainly introduced to solve it 三色标记算法.

The three-color marking algorithm is to mark the objects encountered in the process of traversing objects in the GC Root reachability analysis into the following three colors according to the condition of "visited or not":

  • Black : An object marked black means that it has been accessed by the garbage collector, and all references to the object have been scanned, and it is safe to survive; if there are other object references pointing to the black object, there is no need to re-scan. Black objects can only point to gray objects, not white objects directly.
  • Gray : Objects marked in gray have been visited by the garbage collector, but there are still unscanned references on the object.
  • White : Indicates that the object has not been accessed by the garbage collector; at the beginning of the reachability analysis, all objects are white, if the object is still white after the reachability analysis ends, it means that it is unreachable (that is, garbage objects ).

insert image description here

For example, the following code :

Please add a picture description

Multi-label - floating garbage

During the concurrent marking process, if some local variables (GC Root) are destroyed due to the end of the method running, and the object referenced by this GC Root has been scanned before (marked as a surviving object), then this round of GC will not recycle this part Memory, this part of memory that should have been reclaimed but has not been reclaimed, is called "floating garbage". Floating garbage doesn't affect the correctness of garbage collection, just that 需要等到下一轮垃圾回收中才被清除.

In addition, for new objects generated after concurrent marking (and concurrent cleaning) starts, the usual practice is to directly treat them all as black, and will not be cleared in this round. This part of the object may also become garbage during the period, which is also part of the floating garbage.

Missing label - read and write barrier

Leaking labels will cause the referenced objects to be mistakenly deleted as garbage. This is a serious problem that must be resolved. There are two solutions:增量更新(Incremental Update)和原始快照(Snapshot At The Beginning,SATB)

Incremental update : When the black object inserts a new reference relationship pointing to the white object, after the 将这个新插入的引用记录下来concurrent scanning ends, the black object in the recorded reference relationship is used as the root and re-scanned again. This can be simplified to understand that once the black object is newly inserted with a reference to the white object, it becomes a gray object.

Original snapshot : When the gray object wants to delete the reference relationship pointing to the white object, after 将这个要删除的引用记录下来the concurrent scanning is over, use the gray object in the recorded reference relationship as the root and re-scan again, so that the white object can be scanned Object, directly mark the white object as black (the purpose is to make this object survive the current round of gc cleanup, and rescan in the next round of gc, this object may also be floating garbage)

Regardless of the insertion or deletion of reference relationship records, the record operations of the virtual machine are all implemented through write barriers.

write barrier

The so-called write barrier, to put it simply, is to add some processing before and after the assignment operation (similar to the AOP concept in Spring)

Please add a picture description

  • Write barrier implements SATB (original snapshot)
    When the reference of the member variable of object B changes, for example, the reference disappears (abd = null), we can use the write barrier to record the reference object D of the original member variable of B:
    Please add a picture description

  • Write barrier to realize incremental update
    When the reference of the member variable of object A changes, such as a new reference (ad = d), we can use the write barrier to record the new member variable reference object D of A:
    Please add a picture description

read barrier

Please add a picture description
The read barrier is directly aimed at the first step: D d = abd, when reading member variables, all records are recorded:
Please add a picture description

Modern tracing (based on reachability analysis) garbage collectors almost all borrow the algorithmic idea of ​​​​three-color marking, although the implementation methods are different: for example, white/black collections generally do not appear (but there are other colors that reflect place), the gray collection can be realized by means of stack & queue & cache log, etc., and the traversal method can be breadth & depth traversal, etc.

For the read-write barrier, taking the Java HotSpot VM as an example, the processing scheme for missing marks during concurrent marking is as follows:

  • CMS: write barrier + incremental update
  • G1, Shenandoah: write barrier + SATB
  • ZGC: read barrier

In addition, read-write barriers have other functions. For example, write barriers can be used to record changes in cross-generation/area references, and read barriers can be used to support concurrent execution of moving objects. In addition to functions, there are performance considerations.

Why does the G1 collector use SATB while the CMS collector uses incremental updates?
       Personal understanding: In fact, the biggest difference between the incremental update and the original snapshot is that SATB does not need to deeply scan the deleted reference object again during the remarking phase, while the new update will perform a deep scan on the newly added root object. At the same time, G1 because Many objects are located in different Regions, and CMS is an old generation area. Therefore, re-scanning objects in depth will obviously be more expensive for the structure of the G1 collector. Therefore, G1 chooses SATB and does not scan objects in depth. It is just a simple mark, and waits until the next Round GC in deep scan.

2. Memory set and card list

做GC Root可达性扫描Objects referenced across generations may be encountered during the young generation , such if 又去对老年代再去扫描效率太低. 记录集(Remember Set)For this reason, the data structure ( ) that can be introduced in the new generation 记录从非收集区到收集区的指针集合avoids adding the entire old generation to the GC Root scanning range. In fact, it is not just the problem of cross-generational references between the new generation and the old generation. All garbage collectors involving partial area collection (Partial GC), such as G1 and ZGC collectors, will face the same problem. In the garbage collection scenario, the collector only needs to determine whether there is a pointer to the collection area in a certain non-collection area through the memory set, and does not need to know all the details of the cross-generation reference pointer .

Hotspot uses a 卡表(Cardtable)method called to implement memory sets, which is also the most commonly used method at present.

insert image description here

The card table is implemented using a byte array: CARD_TABLE[], each element corresponds to a memory block of a specific size in the identified memory area, which is called a "card page"; the card page used by HotSpot is 2 9 in size, that is, 512 bytes.

A card page can contain multiple objects. As long as there is a cross-generation pointer in the field of an object, the element ID of the corresponding card table will become 1, indicating that the element is dirty, otherwise it will be 0; The dirty elements in the card table in the collection area are added to GCRoots.


Maintenance of card table:
       dirty card table has been mentioned above, but you need to know how to make the card table dirty, that is, how to update the corresponding ID of the card table to 1 when the reference field assignment occurs, and Hotspot uses a write barrier to maintain the status of the card table.


references:

"In-depth understanding of Java virtual machine"

Guess you like

Origin blog.csdn.net/qq_40436854/article/details/130038215