当前位置:网站首页>JVM garbage collector concept

JVM garbage collector concept

2022-07-06 04:14:00 xmh-sxh-1314

 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeG1oLXN4aC0xMzE0,size_17,color_FFFFFF,t_70,g_se,x_16 Chapter one 《Jvm Garbage collector ( The basic chapter )》 Mainly about the life and death of the judgment object ? Two basic algorithms for judging the life and death of objects 、 Reference counting 、 Reachability analysis algorithm , Method area recovery . In the second part 《Jvm Garbage collector ( Algorithm )》 Several common algorithms of garbage collection are introduced : Mark - eliminate 、 Copy algorithm 、 Mark - Sorting algorithm 、 Generational collection algorithm . So let's focus on Jvm The garbage collector (serial The collector 、parnew The collector 、parallel scavenge The collector 、serial old The collector 、parallel old The collector 、cms The collector 、g1 The collector ). So much has been said to pave the way for it .

 

 

Look at the diagram before entering HotSpot Collectors contained in virtual machines :

 

 

 

The picture shows 7 It works on collectors of different generations , If there is a connection between the two collectors , So they can be used together . The region of the virtual machine indicates whether it belongs to the collector of the new generation or the old generation .

 

Cenozoic collector :Serial、ParNew、Parallel Scavenge

 

Old age collector :CMS、Serial Old、Parallel Old

 

The whole heap collector : G1

 

Several related concepts :

Parallel collection : Multiple garbage collection threads working in parallel , But the user thread is still waiting .

 

Concurrent collection : It means that the user thread and garbage collection thread work at the same time ( It doesn't have to be parallel. It may be executed alternately ). The user program is running , And the garbage collector runs in another CPU On .

 

throughput : namely CPU Time used to run user code and CPU Ratio of total elapsed time ( throughput = Run user code time / ( Run user code time + Garbage collection time )). for example : Virtual machines run together 100 minute , The garbage collector is wasted 1 minute , So throughput is 99%

 

One :Serial The collector

Serial The collector is the most basic 、 Development of the oldest collector .

 

characteristic : Single thread 、 A simple and efficient ( Compared with the single thread of other collectors ), For a limited single CPU In terms of environment ,Serial The collector has no overhead of thread interaction , Focusing on garbage collection can naturally achieve the highest single thread mobile phone efficiency . When the collector is recycling , All other worker threads must be suspended , Until it's over (Stop The World).

 

Application scenarios : Apply to Client Virtual machine in mode .

 

Serial / Serial Old Schematic diagram of collector operation

 

 

 

 

 

Two :ParNew The collector

ParNew The collector is essentially Serial Multithreaded version of collector .

 

Except for multithreading, the rest of the behavior is the same as Serial The collector is as like as two peas ( Parameter control 、 Collection algorithm 、Stop The World、 Object allocation rules 、 Recycling strategy, etc ).

 

characteristic : Multithreading 、ParNew The number of collection threads opened by the collector by default is the same as CPU Same number of , stay CPU In a lot of environments , have access to -XX:ParallelGCThreads Parameter to limit the number of threads for garbage collection .

 

    and Serial There are collectors Stop The World problem

 

Application scenarios :ParNew Collectors are many that run in Server The preferred next generation collector for virtual machines in mode , Because it is in addition to Serial Outside the collector , The only one with CMS The collector works with .

 

ParNew/Serial Old The operation diagram of the combined collector is as follows :

 

 

 

 

 

3、 ... and :Parallel Scavenge The collector

Closely related to throughput , It is also called throughput first collector .

 

characteristic : Belong to the new generation collector is also the collector using replication algorithm , It's also a parallel multithreading collector ( And ParNew Collector similar ).

 

The goal of this collector is to achieve a controllable throughput . There is also a point worthy of attention :GC Adaptive adjustment strategy ( And ParNew The most important difference between collectors )

 

GC Adaptive adjustment strategy :Parallel Scavenge The collector can be set -XX:+UseAdptiveSizePolicy Parameters . There is no need to manually specify the size of the new generation when the switch is on (-Xmn)、Eden And Survivor The proportion of (-XX:SurvivorRation)、 The target age of promotion in the old age (-XX:PretenureSizeThreshold) etc. , The virtual opportunity collects performance monitoring information based on the health of the system , Set these parameters dynamically to provide optimal pause time and maximum throughput , This regulation is called GC The adaptive adjustment strategy of .

 

Parallel Scavenge The collector uses two parameters to control throughput :

 

XX:MaxGCPauseMillis Control the maximum garbage collection pause time

 

XX:GCRatio Set the throughput directly .

 

Four :Serial Old The collector

Serial Old yes Serial Old age version of collector .

 

characteristic : It's also a single thread collector , Using tag - Sorting algorithm .

 

Application scenarios : Mainly used in Client In virtual machine mode . Also available at Server Use... In mode .

 

Server The two main uses of the model ( Explain in detail in the following ···):

 

stay JDK1.5 As well as previous versions of the Parallel Scavenge Collector used with .

As CMS The backup plan of the collector , In concurrent collection Concurent Mode Failure When using .

Serial / Serial Old Working process diagram of collector (Serial The collector diagram is the same ):

 

 

 

5、 ... and :Parallel Old The collector

yes Parallel Scavenge Old age version of collector .

 

characteristic : Multithreading , Using tag - Sorting algorithm .

 

Application scenarios : Focus on high throughput and CPU Where resources are sensitive , You can give priority to Parallel Scavenge+Parallel Old The collector .

 

Parallel Scavenge/Parallel Old Working process diagram of collector :

 

 

 

6、 ... and :CMS The collector

A collector that aims to obtain the shortest recovery pause time .

 

characteristic : Based on tags - Clear algorithm implementation . Concurrent collection 、 Low pause .

 

Application scenarios : For service-oriented response speed , I hope the system has the shortest pause time , Give users a better experience and so on . Such as web Program 、b/s service .

 

CMS The collector runs as follows 4 Step :

 

Initial marker : Mark GC Roots Objects that can be reached directly . It's fast but it's still there Stop The World problem .

 

Concurrent Tags : Conduct GC Roots Tracing The process of , Find the living object and the user thread can execute concurrently .

 

Re label : In order to fix the tag record of the part of the object whose tag changes because the user program continues to run during the concurrent tag . There is still Stop The World problem .

 

Concurrent elimination : Clean up and recycle marked objects .

 

 CMS The collector's memory recovery process is executed concurrently with the user thread .

 

 CMS Working process diagram of collector :

 

 

 

CMS Disadvantages of collectors :

 

Yes CPU Resources are very sensitive .

Can't handle floating garbage , May appear Concurrent Model Failure Failure leads to another Full GC The birth of .

Because of the use of markers - So there will be the problem of space debris , Causes large objects to be unable to allocate space , You have to trigger it one time in advance Full GC.

7、 ... and :G1 The collector

A garbage collector for server applications .

 

The characteristics are as follows :

 

Parallelism and concurrency :G1 Make the most of CPU、 Hardware advantages in a multi-core environment , The use of multiple CPU To shorten Stop-The-World Pause time . Part of the collector needed to stop Java Thread to execute GC action ,G1 The collector can still allow for concurrency Java The program continues to run .

 

Generational collection :G1 Be able to manage the whole Java Pile up , And different ways to deal with newly created objects and have been alive for a while 、 Through many times GC For a better collection .

 

Spatial integration :G1 There will be no space debris during operation , After collection, it can provide regular available memory .

 

Predictable pauses :G1 In addition to pursuing a low pause , It can also establish a predictable pause time model . Enables the user to specify explicitly at a length of M In milliseconds , The time consumed in garbage collection shall not exceed N millisecond .

 

G1 Why can we build a predictable pause time model ?

 

Because it has a plan to avoid in the whole Java Garbage collection in the whole area .G1 Keep track of each Region The size of the garbage inside , Maintain a priority list in the background , Each time according to the allowed collection time , Priority recycling the most valuable Region. In this way, the collection efficiency can be as high as possible in a limited time .

 

G1 The difference from other collectors :

 

Other collectors work for the entire new or old generation 、G1 The working range of the collector is the whole Java Pile up . In the use of G1 When collecting , It will be the whole Java The heap is divided into several independent areas of equal size (Region). Although the new generation of 、 The concept of the old age , But the new and the old are no longer isolated , They are all part of Region( There is no need for continuity ) Set .

 

G1 Problems with the collector :

 

Region It can't be isolated , Distribution in Region Objects in can be associated with Java Any object in the heap has a reference relationship . When the reachability analysis algorithm is used to determine whether the object is alive , You have to scan the whole thing Java Heap to ensure accuracy . Other collectors have this problem (G1 It's just more prominent ). It can lead to Minor GC Decline in efficiency .

 

G1 How the collector solves the above problems ?

 

use Remembered Set To avoid full heap scanning .G1 Each of them Region There's a counterpart to that Remembered Set, The virtual machine finds that the program is correct Reference Type when writing , Will produce a Write Barrier Temporarily interrupt the write operation , Check Reference Whether the reference object is in multiple Region in ( That is to check whether the old generation quoted the new generation of objects ), If it is , Then through the CardTable Records the relevant reference information to the referenced object Region Of Remembered Set in . When doing a memory recall , stay GC Add... To the enumeration range of the root node Remembered Set It can ensure that the whole heap will not be scanned without omission .

 

If maintenance is not calculated Remembered Set The operation of ,G1 The collector can be roughly divided into the following steps :

 

Initial marker : Mark only GC Roots Objects that can be reached directly , And modify TAMS(Next Top at Mark Start) Value , Let the next phase of the user program run concurrently , Can be used in the right Region Create a new object in .( Need thread pause , But it takes a short time .)

 

Concurrent Tags : from GC Roots Start the reachability analysis of the objects in the heap , Find out who's alive .( Longer time consuming , But it can be executed concurrently with the user program )

 

Final marker : In order to fix the part of the tag record that changes due to the execution of the user program during the concurrent tag . And the change of the object is recorded in the thread Remembered Set Logs Inside , hold Remembered Set Logs The data in it is merged into Remembered Set in .( Need thread pause , But it can be executed in parallel .)

 

Screening and recovery : To each Region Sorted by recovery value and cost , According to the user's expectation GC Pause time to plan recovery .( Can be executed concurrently )

 

G1 Schematic diagram of collector operation :

原网站

版权声明
本文为[xmh-sxh-1314]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132236351158.html