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

JVM garbage collector part 2

2022-07-06 17:14:00 Smart popcorn

The focus of this article is to introduce in detail the garbage collector mentioned in the previous article

1. Serial GC For single core and serial

I've said before , This is the oldest garbage collector , Because at that time, they were all single core CPU, A thread can only run user threads or garbage collection threads , Not at the same time , The process is shown in the figure below .

image.png

So this one GC In an exclusive way , For the new generation, it is called SerialGC, Using a replication algorithm , This can improve the recycling efficiency , The old generation is called Serial Old GC, It's using Label compression algorithm , This is to remove memory fragments .

Its advantage lies in , It is suitable for applications with small memory consumption , The only choice for a single core server .

The disadvantages are obvious , Throughput is not high , And in GC When recycling, it will trigger STW, Cause program jam .

Use : We can do it in JVM The parameters are set as follows , Make the new and old times are used SerialGC.

-XX:+UseSerialGC

2. Parallel ParNew GC

His bottom is almost the same as Serial GC It's the same , The only difference is that it can perform garbage collection in parallel , As shown in the figure below .

image.png

The parallel here , It's not that it won't cause STW, This causes the user thread to get stuck , This means that multiple threads perform garbage collection at the same time , It improves the efficiency a lot .

characteristic : It can only be applied to the new generation , For the elderly , Need to cooperate with others GC Use it together , Take another look at the combination diagram .

image.png

You can see , And Serial Old GC Combination can improve efficiency in the new generation , Save thread resources in the elderly generation , It's just that I always feel strange , So in jdk9 Just remove their connection , and jdk14 And... Are removed CMS The connection of , So it's a very embarrassing garbage collector .

Use : We can do it in JVM The parameters are set as follows , Make the new and old times are used SerialGC.

-XX:+UseParNewGC

3. Throughput first parallelism Parallel GC

The recycler is also divided into two types , Used in the Cenozoic Parallel Scavenge GC, Using replication algorithm . Used in the old days Parallel Old GC, Using label compression algorithm , Both are parallel , The operation process is shown in the figure below .

image.png

I think the front ParNewGC It's an imperfect recycler , So the new and old generations use parallel , One uses serial , It feels like Serial A parallel recycler developed for repeatedly building wheels on , So the bottom layer of the new generation of parallel recyclers is different from that before , Better ,JDK8 These two are also used as the default recyclers .

advantage : For pause time , Controls such as throughput are more flexible , It can be adjusted with parameters , There are also adaptive adjustment strategies .

Use : We can do it in JVM The parameters are set as follows , bring In both old and new times ParallelGC, Activate a , The other will also be activated .

-XX:+UseParallelGC
-XX:+UseParallelOldGC

Can be set up The number of young generation garbage collection threads , By default When CPU The number is less than 8, The number of threads is equal to the number of cores .

When CPU Number greater than 8, Choose according to the following formula 3+[(5*cpu_count)/8], Such as cpu The number of 12 individual , The number of threads is 10 individual .

Set as follows , In fact, the default is ok .

-XX:ParallelGCThreads=xx

Adaptive adjustment strategy , Because of this strategy , Cause us to actually view Eden The ratio of the area to the surviving area ranges from 8:1:1 Change into 6:1:1, Its function is to adjust the proportion of memory to reach the balance point , General default .

-XX:+UseAdaptiveSizePolicy

4. Real concurrency CMS GC

CMS Its full name is Concurrent Mark Sweep, That is, concurrent mark removal , Different from before , Due to the need to clean up memory fragments , So the previous algorithms are all based on tag compression , And the GC Not used , Instead, it uses a mark removal algorithm , Because it focuses on shorter stw, That is, the program pause time , Therefore, the overall method is concurrent , That is, the user thread and garbage collection thread execute alternately , The execution process is as follows .

image.png

Workflow

1. Initial marker , from GCRoots Mark reachable objects , Although there is a pause , But the time is short .

2. Concurrent Tags , And GC Threads alternate with user threads , Traverse all reachable objects in the marked memory , Although it takes a long time , But there is no pause .

3. Re label , Fixed objects that changed due to references during the concurrent marking phase , That is, from GCRoots set out , Mark the associated object , Objects that lose references will not be cleared this time , This pause time will be slightly longer than the initial mark .

4. Concurrent elimination , Remove objects that are already garbage , Free up memory .

Be careful

1. Because garbage collection threads almost always recycle , Therefore, it is necessary to recycle in advance before the memory space is insufficient , That is, set a threshold , It's too late to recycle when it's full , Not only will it report concurrent mode failure Error of , It will also start the backup scheme ,Serial To recycle garbage in the old days , So the pause time is long .

2. Because it adopts the mark removal algorithm , So there is the problem of memory fragmentation , You need to maintain a free list record to reasonably select memory allocation .

3. There is a reason not to use the tag compression algorithm , Because the memory address of the object will be changed during compression , And the object may be in use , So this is a last resort , Of course , You can also set the number of times after which tag collation is performed , Reduce debris .

summary

advantage : Short pause time , Effectively utilize thread resources .
shortcoming :
1. Memory fragmentation will occur , It may cause that large objects cannot be put , And the memory space is sufficient , That is, implicit memory leakage .
2. Reduced throughput , Originally, all threads ran user programs at the same time , Now you need to take out a piece of recycled garbage .
3. Produce floating waste , Look back at the first part of the workflow 2 strip , In the concurrent marking phase, if some objects lose references , It won't happen this time GC Is recycled , But wait until next time GC Cleaned up .

5. The mainstream of the future G1

Let's start with two questions , Draw a brick to draw a stone .

Why would there be G1?

We know , Memory and CPU In fact, it is expanding , In this way, we should try our best to improve the throughput , Reduce the pause time of the program , therefore G1 Our goal is to improve the throughput of the full-featured garbage collector as much as possible with controllable delay , The new and old generations are all completed by it .

In fact, it's plain , Previous GC The recycler is still not good enough , We should expand new ideas .

Why is it called G1?

Its characteristic is to adopt new ideas , Partition algorithm , Divide the memory into small pieces , Give priority to the area with the largest garbage collection , So called Garbage First, It is naturally prepared for high-capacity multi-core servers , Small capacity does not reflect its superiority .

G1 The advantages of

1. Both concurrency and parallelism , Use the parallel method where there must be a pause , Increase of efficiency , In most cases, concurrency is used , So that the program does not need to pause .
2. Still a generational collector , Old and new times are divided into small pieces , Let it do all the work alone .
3. Use the copy algorithm to copy the available objects to other areas , But due to blocking , No memory fragmentation .
4. According to the set time , Only the areas with the greatest value are recycled each time , Ensure its maximum recycling efficiency .

region Area

As shown in the figure below , These small pieces , All the same size , May be 1M,2M, Of course, there is an exception , It's the big object , Only the size exceeds 1.5 individual region After the space of , Will be put into H District , There is a reason to put big objects here , If it is a large object used for a short time , Put it directly into the older generation , In fact, it is equivalent to a memory leak , Because it will not be used later , Wasted space .

When it comes to recycling , Some areas have been recycled , It will become a blank area in the figure , These areas are recorded by a blank list , Next time someone comes, put it in it .

image.png

Garbage collection process

The recycling process is as follows , Here is a brief talk about .
1. The younger generation GC: When Eden The district is full , Trigger youngGC, Start parallel exclusive recycling , At the same time, the object is S District ,O Zone move .
2. The younger generation + Concurrent Tags : When the heap space occupation reaches the default threshold 45% when , Trigger concurrency flag , Mark objects that still need to be used .
3. Mixed recycling : Because it is sub regional , Areas marked as garbage will be recycled , It's just that the elderly generally recycle less .

image.png

6. Seven classics GC Summary of

image.png

Actually, it's all in the picture , A few simple points .

Single core , Small memory , Using serial GC, Such as Serial series .
Multicore , Need to improve throughput , Use parallel GC, Use Parallel series .
Multicore , Need low latency , Use concurrency GC, Such as CMS.
Internet , It is generally used G1.

7. Future outlook :shenandoahGC And ZGC

stay openJDK12 in , A new generation of shenandoahGC, This was not officially recognized at that time , It is said that the pause time is 10ms within , But it's just called , But experiments show that the pause time is better than others GC Be excellent , But the throughput will be reduced , That is, on the basis of shortened pause time , Garbage collection runs longer .

although jdk14 And joined in shenandoahGC, But compared with cross era ZGC, Any garbage collector will be eclipsed , It is the real pause time controlled in 10ms within , And I looked jdk17 Has been able to control in 1ms Inside , Throughput is also similar to other GC The same , future , It must be ZGC Of the world .

Finally, I want to say something out of the question , I saw one before AliGC, Ali made it , Performance is better than G1 Slightly better , It's already good , But be ZGC Finish blasting , This also caused my reflection , At home , Ali's technicians are top , But abroad ? After all, we are following the old path of others ,AliGC Just changed G1 The bottom of the , Optimized according to their scenes , But always follow behind others , So we should look further , Don't be a hero at home , And compare with better people abroad , Only in this way can we make greater progress , Be more aware of your shortcomings , Think of musk , His eyes are no longer in this world , But the universe !

ps: temporary JVM My knowledge is over , Then update bytecode Correlation .

原网站

版权声明
本文为[Smart popcorn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060928058192.html