当前位置:网站首页>Garbage collector
Garbage collector
2022-06-21 17:18:00 【What's so strange!】
List of articles
- 1、GC Classification and performance indicators
- 2、 Overview of different garbage collectors
- 3、Serial Recyclers : Serial recovery
- 4、ParNew Recyclers : Parallel recycling
- 5、Parallel Recyclers : Throughput priority
- 6、CMS Recyclers : Low latency
- 7、 Summary of the basic collector
- 8、G1 Recyclers : Regionalized generational
- 9、G1 A detailed description of the garbage collection process
- 10、 Classic garbage collector summary
- 11、 Garbage collectors in the new era
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

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 collectorAlternate with application threads , To minimize application pause timeExclusive garbage collector(Stop the world) Once the run , Stop all user threads in the application , Until the garbage collection process is complete .

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
throughput : The percentage of time spent running user code in total runtime( Total operation time = Program running time + Garbage collection time )
Garbage collection expenses : Throughput complement , The ratio of garbage collection time to total running time
Pause time : When performing garbage collection , The time when the program's worker thread is suspendedCollection frequency : Relative to the execution of the application , Frequency of collection operations
Memory footprint :Java The amount of memory the heap occupiesFast : 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 :
- 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
- 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 .


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
1991 along with JDK1.3.1 Serial collector published together
Serial GCIt's the first one GC.ParNewThe garbage collector is Serial Multithreaded version of collector , A parallel collector2002
Parallel GCandCMS GCFollow JDK1.4.2 ReleaseParallel GCstay JDK1.6 Then become HotSpot VM OfDefault GC2012, since JDK1.7u4 In the version ,
G1You can use2017,
JDK9 in G1 Become the default garbage collector, Replace CMS2018,JDK10 in
G1 With parallel full garbage collection2018,JDK11, introduce
EpsilonRecyclers Simultaneous introductionZGC( Scalable low latency garbage collector ) Not actually installed yet2019,JDK12 It's enhanced G1 Introduced
Shenandoah GC( Low latency GC)2019,JDK13
Enhanced ZGCIt can automatically return unused heap memory to the operating system2020,JDK14
Deleted CMSExpanded 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

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

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
Serial The recycler is the most basic , The oldest garbage collector , stay JDK1.3 The only option to recycle the younger generation before
Serial The collector acts as HotSpot in Client The default young generation garbage collector in mode
Serial Recyclers
Using replication algorithm,Serial recoveryandSTW The mechanism ofYesThe younger generationFor memory recyclingExcept for the younger generation ,Serial The recycler is also dedicated to
Recycling old generation garbageOfSerial OldRecyclers , Serial Old UseMark - Compression algorithm,Serial recoveryas well asSTWThe mechanism is rightOld ageFor memory recyclingSerial Old yes
Running on the Client PatternNextDefaultThe old garbage collectorSerial Old stay Server There are two main uses in this mode :
- And the new generation of Parallel Scavenge In combination with
- As an older generation CMS The backup garbage collection scheme of the collector

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 :
advantageSimple 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

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 achieveControllable throughput, It is calledThroughput priorityThe garbage collector Adaptive adjustment strategyIt'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

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

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
Even though CMS Using concurrent recycling , however
Initial markerandRe labelThese two stages still needSTWPause 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 clearedbecause
The most time-consuming concurrent marking and concurrent cleanupAnd user threadsConcurrent execution, therefore CMS The overall recycling isLow latencyOfBecause 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 OldRecycle the old generation againCMS Use
Mark - eliminateAlgorithm , It means that after each cleaning, aMemory fragments, Therefore use CMS The heap of the collector can only be used to allocate spaceFree listTo 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 ?
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 collectorWhy 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

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

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 RecyclingCopyAlgorithm , But on the whole, it can be seen asMark - CompressAlgorithm , 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 advantagesPredictable 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 :
Turn on G1 Garbage collector (JDK9 Before )
-XX:+UseG1GCSet the maximum memory of the heap
-XmxSet 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
- G1 Mainly for server-side applications , For having large memory , Multiprocessor machines ( It doesn't perform well in a normal size heap )
- 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
- Replace CMS Recyclers , In the following case , Use G1 It's better than using CMS Better
- exceed 50% Of Java The heap is occupied by active data
- The frequency of object allocation or age promotion varies greatly
- GC The pause time is too long ( Longer than 0.5s To 1s)
- 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

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

G1 The garbage collection process :
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
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
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

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

Young GC The specific process of :
Scan the root
take GC Root and Rset As GC Roots, As the root of reachability analysis algorithm
to update Rset
Update complete Rset after ,Rset It can accurately reflect the reference of the old age to the young generation
Handle Rset
Identify objects that are directed by older generation objects Eden Objects in the , These objects are thought to survive
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
Handling references
Handle Soft,Weak,Phantom,Final,JNI Weak Etc , Finally empty Eden,GC Stop working
9.2 Concurrent marking process
Initial marker
Mark objects directly reachable from the root node , here STW, And trigger once Young GC
Root area scan
scanning Survivor Old age objects directly accessible in the district , And mark the referenced object
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 )
Re label
STW Fix the result of concurrent marking ,G1 It used the ratio CMS Faster initial snapshot algorithm (SATB)
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
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

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 :
- , Not enough Region As S The storage area passes through Young GC after Eden The object of survival in
- 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

How to choose a garbage collector ?
Size the heap first so that JVM Adaptive completion
If the heap memory is less than 100M, Use a serial recycler
If it's a single core , Stand alone program , And not sensitive to delay , Use a serial recycler
If it's multi-core , High throughput is required , Select the parallel collector
If it's multi-core , Pursue low delay , Use concurrent collector
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
边栏推荐
- Reinforcement learning introductory project spinning up (1) installation
- Calculation of carbon emissions
- Do Internet companies do unit tests? Is it necessary to do unit testing for the needs of the bank?
- 阿里云服务器+宝塔面板+无域名部署web项目
- Yaml数据驱动演示
- Disruptor local thread queue_ Workprocessor exception_ Fatalexceptionhandler--- inter thread communication work note 004
- 力扣解法汇总1108-IP 地址无效化
- Google Earth engine (GEE) - use sentinel-2 data acquisition to obtain the NDVI difference of one month ago (Guatemala as an example)
- 容器云是什么意思?与堡垒机有什么区别?
- About product operation strategy
猜你喜欢

Beaucoup de sociétés de logiciels sont en fait des "blagues"

未定义的函数或变量【一文讲透】(Matlab)
![[ROS2 基础] Navigation2 导航系统介绍](/img/7f/ad6af972460d7a78fb28d9b4c24477.png)
[ROS2 基础] Navigation2 导航系统介绍

UDP和TCP的对比

Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers

The release of autok3s v0.5.0 continues to be simple and friendly

Yaml file details

Machine learning model monitoring (Apria)

Pytest--生成测试报告

进击的程序员,如何提升研发效能?|直播预告
随机推荐
Postman基本操作
机器学习模型监控(Aporia)
进击的程序员,如何提升研发效能?|直播预告
Unittest框架
qtcreator报错解决
Many software companies are actually "jokes"
力扣解法汇总1108-IP 地址无效化
In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
Necessary for data analysis: 6 steps +5 types +2 analysis methods
Come and watch – tpt18 new report
Actual combat - store login test
Google Play Academy 组队 PK 赛,正式开赛!
面向流动人口管理的人脸验证系统设计及实现 论文+答辩PPT+项目工程文件
【1108. IP 地址无效化】
AutoK3s v0.5.0 发布 延续简约和友好
Do Internet companies do unit tests? Is it necessary to do unit testing for the needs of the bank?
Unittest framework
[ROS2 基础] Navigation2 导航系统介绍
[mathematical modeling] Matlab Application Practice Series (95) - application cases of time series prediction (with matlab code)
Machine learning model monitoring (Apria)