当前位置:网站首页>Garbage collector

Garbage collector

2022-06-21 17:18:00 What's so strange!

List of articles

1、GC Classification and performance indicators

1.1 The classification of garbage collectors

GC It can be a verb (Garbage Collection) garbage collection , It can also be a noun (Garbage Collector) Garbage collector , It depends on the specific use environment , The garbage collector is not specified too much in the specification , It can be made by different manufacturers 、 Different versions JVM To achieve . because JDK The version of is in the process of high-speed iteration , therefore Java So far, it has derived a lot of GC edition , Analyze the garbage collector from different angles , Can be GC There are different types

1.1.1 By thread count

According to the number of threads , Can be divided into Serial garbage collector and Parallel garbage collector

Blue is the user thread , Yellow is the garbage collection thread

 Insert picture description here

Serial recovery It means that only one is allowed in the same time period CPU Used to perform garbage collection operations , At this point, the worker thread is suspended , Until the garbage collection is over .

Parallel collection Multiple can be used CPU At the same time, garbage collection , So it improves the throughput of the application , However, parallel recycling is still the same as serial recycling , Use exclusive , Used “Stop-the-world” Mechanism

  • In the single CPU Hardware platforms such as processors or smaller applications are not particularly advantageous , The performance of the serial collector can surpass that of the concurrent collector and the concurrent collector . therefore , Serial recycling is applied to the client by default Client Mode of JVM in
  • In the case of strong concurrency CPU On , The pause time of parallel collector is shorter than that of serial collector

1.1.2 According to working mode

According to the working mode , Can be divided into Parallel garbage collector and Exclusive garbage collector

  • Parallel garbage collector Alternate with application threads , To minimize application pause time
  • Exclusive garbage collector (Stop the world) Once the run , Stop all user threads in the application , Until the garbage collection process is complete .

 Insert picture description here

1.1.3 According to the way of fragment processing

According to the way of fragmentation , Can be divided into Compression garbage collector and Uncompressed garbage collector

  • After the compression garbage collector is recycled , Compress the living objects , Remove the recovered debris
  • The uncompressed garbage collector does not perform this operation

1.1.4 By working memory range

By working memory range , Can be divided into The younger generation of garbage collectors and Old garbage collector

1.2 assessment GC Performance index of

  1. throughput : The percentage of time spent running user code in total runtime

    ( Total operation time = Program running time + Garbage collection time )

  2. Garbage collection expenses : Throughput complement , The ratio of garbage collection time to total running time

  3. Pause time : When performing garbage collection , The time when the program's worker thread is suspended

  4. Collection frequency : Relative to the execution of the application , Frequency of collection operations

  5. Memory footprint :Java The amount of memory the heap occupies

  6. Fast : The time from the birth of an object to its recycling

throughput 、 Pause time 、 Memory footprint Are the three most important indicators , These three constitute a “ Impossible Triangle ”, Any kind of garbage collector can not meet all the requirements

Of the three , The importance of delay is becoming increasingly prominent , Because with the development of hardware , More memory usage is becoming more and more tolerable , The improvement of hardware performance also helps to reduce the impact of the garbage collector runtime on user threads , That is, the throughput can be improved , But memory expansion , Instead, let go GC The delay is longer

So when designing a garbage collector , Now we mainly consider two points : throughput and Pause time

1.3 throughput vs Pause time

  • throughput :CPU Running the user program takes up CPU Ratio of total elapsed time , The higher the throughput, the better

    • throughput = Time to run user program / ( Time to run user program + Garbage collection time )
    • High throughput applications make users feel that the program is always working , It's going on “ production ”
  • Delay Conduct GC In time , The user thread stops working , This is the delay

    • The lower the delay, the better , Low latency applications allow users to have a smooth interactive experience

High throughput and low latency are contradictory , because :

  1. If the program is required to have high throughput , Then we must reduce GC The frequency of , But this can lead to a single GC Use more time , Resulting in a longer delay
  2. If the program is required to have a low delay , We need to improve GC The frequency of , A single GC The time is shorter , Delay reduction , But this can lead to the whole GC Take up more time , Results in reduced program throughput .

 Insert picture description here

 Insert picture description here

Standard for modern recyclers : In the case of maximum throughput priority , Reduce delay time

2、 Overview of different garbage collectors

2.1 The history of garbage collector

With JVM, You must have a garbage collection mechanism , namely Garbage Collection, The corresponding product is called Garbage Collector

  1. 1991 along with JDK1.3.1 Serial collector published together Serial GC It's the first one GC.ParNew The garbage collector is Serial Multithreaded version of collector , A parallel collector

  2. 2002 Parallel GC and CMS GC Follow JDK1.4.2 Release

  3. Parallel GC stay JDK1.6 Then become HotSpot VM Of Default GC

  4. 2012, since JDK1.7u4 In the version ,G1 You can use

  5. 2017,JDK9 in G1 Become the default garbage collector , Replace CMS

  6. 2018,JDK10 in G1 With parallel full garbage collection

  7. 2018,JDK11, introduce Epsilon Recyclers Simultaneous introduction ZGC( Scalable low latency garbage collector ) Not actually installed yet

  8. 2019,JDK12 It's enhanced G1 Introduced Shenandoah GC( Low latency GC)

  9. 2019,JDK13 Enhanced ZGC It can automatically return unused heap memory to the operating system

  10. 2020,JDK14 Deleted CMS Expanded ZGC stay macOS and Windows Application on

Seven classic garbage collectors :

  • Serial recycler :Serial、Serial Old
  • Parallel recycler :ParNew、Parallel Scavenge、Parallel Old
  • Concurrency collector :CMS、G1

2.2 Recycling area of classic recycler

 Insert picture description here

  • Younger generation recyclers : Serial, ParNew, Parallel Scavenge

  • Old age collector : Serial Old, Parallel Old, CMS

  • The whole heap collector : G1

2.3 The composition of garbage collectors

 Insert picture description here

Solid line : It shows that the two can be used together

Red dotted line : JDK8 Abandoned when ,JDK9 Is removed

Dotted green line : JDK14 Was abandoned

Cyan dotted line : CMS stay JDK14 Be removed in the

Why are there so many garbage collectors , Isn't one enough ?

because Java There are a lot of usage scenarios , Mobile , Server side, etc , So we need to do different scenarios , Provide different garbage collectors , To improve the performance of garbage collection in different scenarios , There is no perfect garbage collector , But there is a garbage collector that works best in certain scenarios

//  See the default garbage collector 
-XX:+PrintComandLineFlags

3、Serial Recyclers : Serial recovery

//  Using parameter 
-XX:+UseSerialGC
// Appoint JVM The younger generation uses SerialGC, Used in the old days Serial Old GC
// These two serial collectors , Just specify one , The other one is used automatically 
  1. Serial The recycler is the most basic , The oldest garbage collector , stay JDK1.3 The only option to recycle the younger generation before

  2. Serial The collector acts as HotSpot in Client The default young generation garbage collector in mode

  3. Serial Recyclers Using replication algorithm , Serial recovery and STW The mechanism of Yes The younger generation For memory recycling

  4. Except for the younger generation ,Serial The recycler is also dedicated to Recycling old generation garbage Of Serial Old Recyclers , Serial Old Use Mark - Compression algorithm , Serial recovery as well as STW The mechanism is right Old age For memory recycling

  5. Serial Old yes Running on the Client Pattern Next Default The old garbage collector

  6. Serial Old stay Server There are two main uses in this mode :

    1. And the new generation of Parallel Scavenge In combination with
    2. As an older generation CMS The backup garbage collection scheme of the collector

 Insert picture description here

Serial/Serial Old Is a single threaded collector , Its single thread meaning not only means that it will only use one thread to complete the recycling work , What's more, when it comes to garbage collection , All other worker threads must be suspended (STW), Until the end of recycling


Serial Compare the advantages and disadvantages of :

  • advantage
    • Simple and efficient ( Compared with other single thread collectors ), For a limited single CPU In terms of environment ,Serial The collector has no overhead of thread interaction , Concentrating on garbage collection can naturally achieve the highest collection efficiency in a single threaded environment
    • Not much memory ( Dozens of MB To one or two hundred MB) In the application scenario of , Garbage collection happens infrequently , Using a serial collector is acceptable .
  • shortcoming
    • With multicore CPU The development of , Serial recyclers are rarely used
    • For interactive applications , Such a collector is not acceptable , Recycling will cause the user thread to stop completely

4、ParNew Recyclers : Parallel recycling

// Use ParNew
-XX:+UseParNewGC
//  Indicates that the younger generation uses parallel collectors , It doesn't affect the older generation 

Serial GC It is the single thread garbage collector in the younger generation , and ParNew The recycler is Serial Multithreaded version of the recycler ,Par yes Parallel Abbreviation ,New Means that only The younger generation ,ParNew In addition to using multithreading for garbage collection , Almost and Serial There is no difference between

ParNew The collector uses a replication algorithm ,STW Mechanism , Garbage collection for the young generation in a parallel way ,ParNew A lot JVM Running on the Server The default garbage collector of the younger generation in mode

 Insert picture description here

ParNew The reclaimer is suitable for running in multiple CPU Under the environment of , Because we can make full use of CPU, Physical hardware advantages such as multi-core , Garbage collection can be completed more quickly , Improve the throughput of the program , But in a single CPU In the environment ,ParNew The recycler is not as good as Serial More efficient , Because single CPU Use Serial The collector does not need to switch threads frequently

except Serial Outside , Only ParNew GC Energy and harmony CMS The collector works with

5、Parallel Recyclers : Throughput priority

// Use Parallel 
-XX:+UseAdaptiveSizePolicy

HotSpot In addition to ParNew The collector is based on parallel recycling ,Parallel The collector also uses a replication algorithm , Parallel recycling and STW Mechanism to recycle the young generation

Parallel The emergence of is not unnecessary ,

  • And Parallel and ParNew Different ,Parallel The goal of the collector is to achieve Controllable throughput , It is called Throughput priority The garbage collector
  • Adaptive adjustment strategy It's also Parallel scavenge And ParNew An important difference

High throughput can make efficient use of CPU Time , Complete the operation task of the program as soon as possible , It is mainly suitable for tasks that run in the background without too much interaction , Such as order processing , Payment of wages , Applications such as scientific computing , Adaptive adjustment strategy is also Parallel And ParNew An important difference between

Parallel The recycler is in JDK1.6 Provides for older generation garbage collection Parallel Old Recyclers , Used in substitution Serial Old Recyclers ,Parallel Old The recycler is marked - Compression algorithm , Parallel recycling and STW Mechanism for garbage collection of the elderly generation

 Insert picture description here

In the application scenario of program throughput priority ,Parallel and Parallel Old Used in combination with Server The recycling performance in this mode is very good ,Parallel and Parallel Old Both parallel collectors can set the number of threads for garbage collection , But the number of recycling threads is not the more the better , Too many threads switching back and forth will affect the performance of garbage collection

stay JDK1.8 in , By default Parallel and Parallel Old Combine

6、CMS Recyclers : Low latency

Before learning , First understand the three color mark removal algorithm : Three color mark removal algorithm

-XX:+UseConcMarkSweepGC
//  Enable CMS Recyclers , The younger generation will automatically use ParNewGC

stay JDK1.5 period ,HotSpot Launch a product in Strong interactive applications An epoch-making garbage collector in ,CMS(Concurrent-Mark-Sweep) This recycler yes HotSpot The first true concurrent collector in , For the first time, the user thread and the recycle thread work at the same time

CMS Our focus is to minimize the pause time of user threads during garbage collection , The shorter the pause time ( The lower the delay ) More suitable for programs that interact with users , Good response speed can improve the user experience , At present, a large part of Jaba The program is concentrated on the Internet or B/S On the server side , Such applications pay special attention to the response speed of services , I hope the system will stop as soon as possible , To bring a better experience to users ,CMS To meet the needs of such applications .

CMS Using tag - Clear algorithm , Concurrent execution and STW Mechanism for garbage collection of the elderly generation

CMS The working process of is mainly divided into 4 A step :

Initial marker , Concurrent Tags , Re label , Concurrent cleanup

 Insert picture description here

6.1 Initial marker (Initial-Mark)

During initial marking , The worker thread will be because STW There is a short pause , The main task of this stage Just marking out GC Roots Objects that can be directly related to , Once the marking is complete, all user threads that were previously suspended will be resumed .

Because the directly associated objects are relatively small , All the speed here is very fast .

6.2 Concurrent Tags (Concurrent-Mark)

from GC Roots Of The directly associated object starts traversing other objects attached to the reference chain , This process Longer time consuming however There is no need to pause the user thread , It can be executed concurrently with the user thread

6.3 Re label (ReMark)

Due to the concurrent marking process , User threads and GC Mark threads to execute concurrently , So you need to Mark again before clearing , Ensure that the reference relationship of the object is correct . Relabeling cannot determine what happens to the user thread in the concurrent marking phase " Floating garbage " This stage also needs to be advanced STW, The time is slightly longer than the initial mark , But it is much shorter than the concurrent tag

6.4 Concurrent elimination (Concurrent-Sweep)

This phase cleans up dead objects , because There is no need to move live objects . This phase can also interact with user threads Concurrent execution , But the mark - The purge algorithm produces memory fragmentation

6.5 CMS Characteristics

  1. Even though CMS Using concurrent recycling , however Initial marker and Re label These two stages still need STW Pause user thread , But the pause time will not be too long , All the recyclers can't be unnecessary STW Mechanism , Can only minimize STW Time for , No, STW There is no guarantee that the reference relationship of the object will not change when it is cleared

  2. because The most time-consuming concurrent marking and concurrent cleanup And user threads Concurrent execution , therefore CMS The overall recycling is Low latency Of

  3. Because the concurrent cleanup phase and the user thread execute concurrently , While recycling garbage , While making garbage , therefore stay CMS Concurrent purge phase , You need to make sure that the user thread has enough memory available , therefore CMS Like other collectors, you can't wait until the old age is filled before recycling , But when the usage rate in the old age reaches a certain threshold, it starts to work immediately , If CMS The memory reserved during operation cannot meet the needs of user threads , There will be one "Concurrent Mode Failure" Failure , The occasional Serial Old Recycle the old generation again

  4. CMS Use Mark - eliminate Algorithm , It means that after each cleaning, a Memory fragments , Therefore use CMS The heap of the collector can only be used to allocate space Free list To allocate space for objects , Although the mark - Compression algorithm can avoid memory fragmentation , But it cannot be applied to CMS in , Because in the concurrent cleanup phase , While clearing the dead object , While executing the user thread , If you use a marker - Compression algorithm , Then the objects in the heap will be moved , The reference will change , The executing user thread may not find the object according to the original object address

6.6 Comparison of advantages and disadvantages

advantage :

  • Concurrent recovery
  • Low latency

shortcoming

  • Generate memory fragmentation

    If the elderly are seriously fragmented , At this point, a large object is put into the heap , Will leave early full GC

  • CMS The collector to CPU Resource sensitive

    In the concurrent phase , Although it will not stop the user thread . But it will cause the program to slow down because it occupies part of the threads , Total throughput reduction

  • CMS The collector cannot handle floating garbage

    In the concurrent marking phase, due to user threads and GC Mark threads to execute concurrently , If the concurrent marking phase generates new garbage ,CMS It will not be possible to mark these garbage . The relabel stage can only judge the objects that were suspected to be garbage , As a result, the newly generated garbage can only be recycled the next time

CMS Although it can achieve good low latency , But it has brought a lot of disadvantages , The cleanup phase failed to defragment memory , As a result, the old age was filled with a large number of memory fragments , And when the reserved memory in the old age is insufficient , Will enable Serial Old As a temporary collector , and Serila Old Is a serial exclusive collector , In most current application scenarios , If there is a business peak , Then the user may feel a pause for a few seconds or even longer

stay JDK9 in ,CMS Marked as abandoned , stay JDK14 in CMS Be deleted

7、 Summary of the basic collector

If you want to minimize the use of memory and parallel overhead , choice Serial GC

If you want to maximize the throughput of your application , choice Parallel GC

If you want to minimize GC The interruption or pause time of , choice CMS GC

8、G1 Recyclers : Regionalized generational

8.1 G1 summary

A few questions ?

  1. HotSpot There are already many built-in recyclers , Why do you need to publish G1?

     The reason is that as applications get larger , complex , More and more users 
     No, GC There is no guarantee that the program will work properly , but GC Often caused by STW And can't keep up with the actual demand   That's why I keep being right GC To optimize 
    G1 The recycler is JDK7 The new garbage collector introduced later , It is one of the leading achievements in the development of waste recycling technology 
    
     In order to adapt to the expanding memory and the increasing number of processors , Further reduce delay , Good throughput at the same time 
     Official G1 The goal is to achieve as much throughput as possible with controllable delay 
    G1 It takes on the responsibility and expectation of a full-featured collector 
    
  2. Why is it called Garbage First(G1)?

     because G1 It's a parallel collector , It divides the heap memory into many unrelated areas (Region)  These areas can be physically discontinuous 
     Use different Region To express Eden,S0,S1,Old etc. 
    
    G1 The whole Java Garbage collection in the whole area is carried out in the pile 
    G1 Keep track of each Region The value of the garbage in it ( The amount of space obtained by recycling and the time required for recycling )
     Maintain a priority list in the background , Each time according to the allowed collection time , Priority recycling the most valuable Region
    
     Because this method focuses on the area with the most garbage collected , therefore G1 It can be understood as  Garbage First  That is, garbage first 
    

G1 Is a garbage collector for server-side applications , It is mainly aimed at equipping with multi-core CPU And large memory machines , Satisfy with a high probability GC At the same time as the pause time , It also has the performance characteristics of high throughput

stay JDK9 in the future ,G1 Become HotSpot The default garbage collector for , Take into account the garbage collection of the younger generation and the elderly generation , Officially known as “ A fully functional garbage collector ”

8.2 G1 The advantages of

  • Parallelism and concurrency

    • Parallelism :G1 During recycling , There can be multiple GC Threads work at the same time , Make effective use of multi-core working capability , At this point, the user thread STW
    • concurrency :G1 Have the ability to execute alternately with user threads , Part of the work can be executed simultaneously with the user thread . So in general , There is no complete blocking of the application during the entire recycle phase
  • Generational and partitioned collection

    • In terms of generations ,G1 It is still a generational garbage collector , It will distinguish between the younger generation and the older generation , The younger generation still has Eden and S, But from a heap perspective , It does not require the younger generation or the older generation to be continuous , Also no longer adhere to fixed size and fixed number

       Insert picture description here

    • G1 The heap space is divided into several regions (Region), These areas include the logical younger and older generations

       Insert picture description here

    • Both the young and the old

  • Spatial integration

    G1 Divide the memory into one by one Region, Memory recycling is With Region As a basic unit ,Region Recycling Copy Algorithm , But on the whole, it can be seen as Mark - Compress Algorithm , Both algorithms can Avoid memory fragmentation , This feature is good for the program to run for a long time , Allocating large objects does not trigger the next time in advance because continuous memory space cannot be found GC, especially When Java When the pile is very large ,G1 More obvious advantages

  • Predictable pause time model

    This is a G1 be relative to CMS Another big advantage of ,G1 In addition to pursuing low latency , It can also establish a predictable pause time model , This allows the user to explicitly specify a length of M In milliseconds of time , The time consumed in garbage collection shall not exceed N millisecond

    • Because of the zoning ,G1 You can select only part Region To recycle , This reduces the scope of recycling , Therefore, the situation of pause can be better controlled
    • G1 Keep track of each Region The value of the garbage in it , Maintain a priority list in the background ,** Each time according to the allowed collection time , Priority recycling the most valuable Region,** To ensure the G1 In a limited time, you can
    • be relative to CMS,G1 It may not be possible to achieve the minimum delay , But the worst is much better

8.3 G1 The shortcomings of

Compare with CMS,G1 We don't have all-round , An overwhelming advantage , Such as While the user program is running ,G1 Whether it's for garbage collection, the memory footprint (Footprint) Or the extra execution load of the program runtime (Overload) It's better than CMS high

Empirically , In small memory applications CMS Probably better than G1, and G1 It will play its advantages in large memory applications , The balance point is 6-8G Between

8.4 G1 Common operation steps of

G1 The principle of design is to simplify JVM performance tuning , Tuning is a simple three-step process :

  1. Turn on G1 Garbage collector (JDK9 Before )

    -XX:+UseG1GC
    
  2. Set the maximum memory of the heap

    -Xmx
    
  3. Set up GC Maximum pause time

    -XX:MaxGCPauseMillis
    // The default is 200ms,JVM I will try my best to achieve , But there is no guarantee that 
    

G1 Three kinds of garbage collection modes are provided in ,YoungGC,Mixed GC,Full GC, Triggered under different conditions

8.5 G1 The applicable scenarios of

  1. G1 Mainly for server-side applications , For having large memory , Multiprocessor machines ( It doesn't perform well in a normal size heap )
  2. The main application is to reduce the delay , And a lot of programs ,G1 By cleaning up part at a time rather than the whole Region Incremental cleaning to ensure that every time GC The pause time will not be too long
  3. Replace CMS Recyclers , In the following case , Use G1 It's better than using CMS Better
    1. exceed 50% Of Java The heap is occupied by active data
    2. The frequency of object allocation or age promotion varies greatly
    3. GC The pause time is too long ( Longer than 0.5s To 1s)
  4. stay HotSpot All garbage collectors in , except G1 Outside , Other recyclers use their built-in JVM Threads execute GC operation , and G1 You can use user threads to assume the responsibility of running in the background GC Work , When JVM Of GC When thread processing slows down , The system calls the user thread to help speed up the garbage collection process

8.6 Region Introduction to

Use G1 When collecting , It will be the whole Java The heap is divided into 2048 Independent of the same size Region, Every Region The size of the heap depends on the actual size of the heap space , The whole is controlled in 1MB To 32MB Between , And for 2 Of N The next power , Can pass **-XX:G1HeapRegionSize** Set up , be-all Region Same size , And in JVM It's not going to change in the life cycle

Although still retain the concept of the younger generation and the older generation , But younger and older generations are no longer physically isolated , They are all part of Region( It can be discontinuous ) aggregate , adopt Region The dynamic allocation of the method to achieve logical continuity

 Insert picture description here

One Region May belong to Eden,S, or Old, But one Region It can only belong to one area when it is active , You can't do both at the same time Eden Again Old, In the figure E Indicates that the area belongs to Eden,S Indicates belonging to Survivor,O Indicates belonging to Old,H Express Humongous It mainly stores large objects , If the size of an object exceeds 1.5 individual Region, Just store it in H, Blank indicates unused memory space , stay Region When allocating objects in , use Pointer collision and TLAB

Set up H Why :

For large objects in the heap , By default, it will be directly assigned to the old age . But if it's a short-term big object , It will have a negative impact on the garbage collector . To solve this problem ,G1 Divided Humongous District , Use it to store large objects , If one H There's no room for a big object , that G1 Will look for continuous H Area to store . In order to find a continuous H District , Sometimes you have to start Full GC.G1 Most of the actions of H As a part of the older generation

8.7 G1 The recycling link of the recycler

G1 There are three main steps in garbage collection :

  • The younger generation GC (Young GC)

  • In the old days, concurrent marking processes (Concurrent Marking) At the same time, the young generation GC

  • Mixed recycling (Mixed GC) For the younger generation GC And the older generation GC

Young GC -> Young GC + Cocurrent Marking -> Mixed GC
if necessary , Single thread , exclusive , High intensity Full GC It still exists , It's for GC The evaluation failure of provides a failure protection mechanism , Strong recycling

 Insert picture description here

G1 The garbage collection process :

  1. Light generation Eden When the area memory is exhausted , start-up Yong Gc,G1 Of Young GC Is a parallel exclusive collector , Pause all user threads , Start many GC Thread recycling younger generation , Then move the young surviving objects to S District or Old District

  2. When heap memory usage reaches a certain value ( Default 45%), Start the old age concurrent tagging process . The concurrent marking process is performed at the same time Young GC,GC The thread marks the old generation survival object and executes concurrently with the user thread

  3. Start mixing and recycling immediately after marking

    G1 Move the living objects of the older generation to the free space , These free spaces became part of the old age .

    Unlike the younger generation ,G1 The old age recycler does not need to recycle the whole old generation , Just one scan at a time / Recycle a small part of the old age Region, At the same time Young GC

9、G1 A detailed description of the garbage collection process

 Insert picture description here

9.1 The younger generation GC

JVM Startup time ,G1 Will be ready first Eden District , During the running process of the program, objects are continuously created to Eden in , When Eden When space runs out ,G1 Will start once Young GC,Young GC It's just recycling Eden Area and Survivor District

Young GC when , First GC Meeting STW Stop the execution of the user thread ,G1 Create a recycle set , A recycle set is a collection of memory segments that need to be recycled , The younger generation recycling process includes Eden Area and Survivor Area all memory segments

 Insert picture description here

Young GC The specific process of :

  1. Scan the root

    take GC Root and Rset As GC Roots, As the root of reachability analysis algorithm

  2. to update Rset

    Update complete Rset after ,Rset It can accurately reflect the reference of the old age to the young generation

  3. Handle Rset

    Identify objects that are directed by older generation objects Eden Objects in the , These objects are thought to survive

  4. copy object

    Traverse GC Roots,Eden The surviving objects in are copied to Survivor District ,Survivor The age of the subjects who survived in the area and whose age has not exceeded the threshold value plus 1, Objects whose age exceeds the threshold are assigned to Old

  5. Handling references

    Handle Soft,Weak,Phantom,Final,JNI Weak Etc , Finally empty Eden,GC Stop working

9.2 Concurrent marking process

  1. Initial marker

    Mark objects directly reachable from the root node , here STW, And trigger once Young GC

  2. Root area scan

    scanning Survivor Old age objects directly accessible in the district , And mark the referenced object

  3. Concurrent Tags

    Concurrent marking and concurrent execution of user threads in the whole heap , If you find Region All objects in are garbage , The area will be recycled immediately , The concurrent marking phase calculates each Region Object activity of ( The proportion of living objects in the area )

  4. Re label

    STW Fix the result of concurrent marking ,G1 It used the ratio CMS Faster initial snapshot algorithm (SATB)

  5. Exclusive cleanup

    Calculate the survival objects and GC Recycling ratio , And sort , yes STW Of , Identify areas where recycling can be mixed , This stage will not really recycle

  6. Concurrent cleanup

    Identify and clean up completely free space

9.3 Mixed collection

When more and more garbage enters the old age , It needs to be done once Mixed GC, Avoid running out of memory

Mixed GC There will be one Young GC Recycle the whole young generation , And recycle part of the old time , Not all the older generation , Thus to GC Time control

 Insert picture description here

9.4 Full GC

GC My original intention is to avoid Full GC Appearance , If it cannot work normally after the above three links ,G1 Meeting STW, And use the single thread memory recycling algorithm for garbage collection , The performance will be very poor , The delay is high

Lead to Full GC There may be two reasons for this :

  1. , Not enough Region As S The storage area passes through Young GC after Eden The object of survival in
  2. The space is exhausted before the concurrent marking process is completed

Increase the heap memory size , Can effectively avoid Full GC

10、 Classic garbage collector summary

By JDK1.8, Altogether 7 Different garbage collectors , Each one has different features , In specific use , You need to select different garbage collectors according to specific situations

 Insert picture description here

How to choose a garbage collector ?

  1. Size the heap first so that JVM Adaptive completion

  2. If the heap memory is less than 100M, Use a serial recycler

  3. If it's a single core , Stand alone program , And not sensitive to delay , Use a serial recycler

  4. If it's multi-core , High throughput is required , Select the parallel collector

  5. If it's multi-core , Pursue low delay , Use concurrent collector

  6. Now Internet projects basically use G1, Can control latency and maintain high throughput

There is no best universal recycler , Tuning is only for specific scenarios , Specific needs , There is no recycler once and for all

11、 Garbage collectors in the new era

11.1 The new development of garbage collector

GC It is still developing ,G1 They are also constantly improving , For example, originally G1 Of Full GC stay JDK10 After that, it has been running in parallel , In many scenarios , Its performance is slightly better than that of Parallel GC Parallel of Full GC Realization

Even if it's Serial GC, Old as it is , But simple design and implementation may not be out of date , Its own expenses , Whether it's GC The cost of related data , Or the cost of data structure , Or the overhead of threads , They're all very small , With the rise of cloud computing , stay Serveless In new application scenarios ,Serial GC Found a new stage

Unfortunately CMS, Because of the defect of its algorithm , Although there are still many programs to use now , But in JDK9 Species have been marked as obsolete , And in JDK14 Remove

11.2 Epsilon Recyclers and Shenandoah Recyclers

Epsilon Recyclers : Also known as " No operation reclaimer ", It only does memory allocation , Do not recycle garbage . Applicable to certain allocated spaces , A short application that ends after execution

Shenandoah Recyclers : be called " Scalable low latency garbage collector ". Featured with low delay , Claim that the delay time is independent of the heap size

11.3 Revolutionary ZGC

ZGC And Shenandoah Target highly similar , In the case of as little impact on throughput as possible , The implementation can put... Under any heap size GC The delay time is controlled at 10ms within

ZGC It's based on Region Memory layout , Without generations , Read barrier used , Color pointer and memory multiple mapping technology to achieve Concurrent tags - Of compression algorithm , Garbage collector with low latency as the primary goal

ZGC Almost all links are executed concurrently , Except that the initial tag is STW Of , So almost all the pause time is spent on the initial tag , This part of time is actually very little

原网站

版权声明
本文为[What's so strange!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211343510557.html