当前位置:网站首页>G1 and ZGC garbage collector

G1 and ZGC garbage collector

2022-06-27 06:03:00 The season when the monsoon dies

G1 The collector (-XX:+UseG1GC)
G1 (Garbage-First) Is a server oriented garbage collector , Mainly for machines equipped with multiple processors and large memory . Satisfy with a high probability GC At the same time as the pause time is required , High throughput performance .

      G1 take Java The heap is divided into several independent areas of equal size (Region),JVM Up to 2048 individual Region.
commonly Region Size is the heap size divided by 2048, For example, the heap size is 4096M, be Region The size is 2M, Of course, you can also use parameters "-XX:G1HeapRegionSize" Specify manually Region size , But the default calculation method is recommended .
G1 It retains the concept of the younger generation and the older generation , But it's no longer a physical barrier , They are all ( It can be discontinuous )Region Set .
      The default young generation's share of heap memory is 5%, If the heap size is 4096M, So young generation occupy 200MB Left and right memory , It's about 100 individual Region, Can pass “-XX:G1NewSizePercent” Set the initial proportion of the new generation , In system operation ,JVM Will keep adding more to the younger generation Region, But the largest proportion of the new generation will not exceed 60%, Can pass “-XX:G1MaxNewSizePercent” adjustment . In the younger generation Eden and Survivor Corresponding region It's the same as before , Default 8:1:1, Let's say that the younger generation now has 1000 individual region,eden Area corresponds to 800 individual ,s0 Corresponding 100 individual ,s1 Corresponding 100 individual .
      One Region Maybe it was the younger generation , If Region There was garbage collection , And then it might be the older generation , in other words Region The function of the region may change dynamically .
     G1 The garbage collector has the same principle about when the object will move to the old age , The only difference is the handling of large objects ,G1 There is a special distribution of large objects Region It's called Humongous District , Instead of letting big objects go straight into the old age Region in . stay G1 in , The decision rule of large object is that there is more than one large object Region The size of 50%, For example, according to the above calculation , Every Region yes 2M, As long as a big object exceeds 1M, Will be put in Humongous in , And if a big object is too big , It may span multiple Region To hold the .Humongous The area is dedicated to storing short-term giant objects , Don't go straight into the old days , It can save space for the elderly , To avoid the lack of space for the elderly GC expenses .
      Full GC In addition to collecting the younger generation and the older generation , Will also Humongous We're going to recycle them together .G1 Collector once GC The operation process of is roughly divided into the following steps :
Initial marker (initial mark,STW): Pause all other threads , And record gc roots Objects that can be referenced directly , fast ;
Concurrent Tags (Concurrent Marking): Same as CMS The concurrency flag for
Final marker (Remark,STW): Same as CMS Re marking of
Screening and recovery (Cleanup,STW): The screening recovery phase starts with each Region Of Sort recovery value and cost , According to the user's expectations Looking forward to GC Pause time ( It can be used JVM Parameters -XX:MaxGCPauseMillis Appoint ) To make a recycling plan , For example, the elderly have 1000 individual Region It's full , But because according to the expected pause time , This garbage collection may only stop 200 millisecond , So we know from the previous cost recovery calculation that , Maybe recycle it 800 individual Region Just in time 200ms, Then it will only be recycled 800 individual Region(Collection Set, Collection to recycle ), Try to put GC The resulting pause time is controlled within our specified range . In fact, this stage can be executed concurrently with the user program , But because only one is recycled Region, Time is user controllable , And pausing user threads will greatly improve collection efficiency . Whether it's the younger generation or the older generation , The recycling algorithm mainly uses the replication algorithm , Will a region Copy the surviving object in to another region in , It's not like CMS like that After recycling, because there are many memory fragments that need to be cleaned up ,G1 Using the replication algorithm to recycle almost no memory fragments .( Be careful :CMS The recycle phase is executed concurrently with the user thread ,G1 Because the internal implementation is too complex, no concurrent recovery has been implemented for the time being , But here we are. Shenandoah The garbage collector implements concurrent collection ,Shenandoah It can be seen as G1 Updated version of )

G1 The collector maintains a priority list in the background , Each time according to the allowed collection time , Priority should be given to the most valuable Region( This is its name Garbage-First The origin of ), For example, a Region flowers 200ms Can recycle 10M The garbage , Another one Region flowers 50ms Can recycle 20M The garbage , In return In case of limited collection time ,G1 Of course, the latter one will be preferred Region Recycling . This kind of use Region Partition the memory space and the way to recycle the priority area , To ensure the G1 The collector can be as efficient as possible in a limited time .
G1 Be regarded as JDK1.7 Above version Java An important evolutionary feature of virtual machines . It has the following characteristics :
Parallelism and concurrency :G1 Make the most of CPU、 Hardware advantages in a multi-core environment , The use of multiple CPU(CPU perhaps CPU The core ) To shorten Stop-The-World Pause time . Some of the other collectors would have paused Java Thread to execute GC action ,G1 The collector can still allow for concurrency java Program continues .
Generational collection : although G1 You can manage the whole thing without the cooperation of other collectors GC Pile up , But it retains the idea of generations .
Spatial integration : And CMS Of “ Mark -- clear ” Different algorithms ,G1 On the whole is based on “ Tag to sort out ” Algorithm implementation of the collector ; Locally, it's based on “ Copy ” algorithmic .
Predictable pauses : This is a G1 be relative to CMS Another big advantage , Reduce the pause time G1 and CMS Common concerns , but G1 In addition to pursuing a low pause , Still can build Predictable pause time model , Enables the user to specify explicitly at a length of M Millisecond fragments of time ( Through parameters "-XX:MaxGCPauseMillis" Appoint ) Complete the garbage collection in .
    without doubt , It can be specified by the user that the expected pause time is G1 A powerful feature of the collector , Set different expected pause times , Can make G1 Achieve the best balance between attention throughput and attention latency in different application scenarios . however , Set up here “ Expectations ” It has to be realistic , You can't be whimsical , After all G1 To freeze user threads to copy objects , No matter how low the pause time is, there must be a limit . Its default pause target is 200 milliseconds , Generally speaking , It's normal that the recycle phase takes tens to 100 milliseconds or even nearly 200 milliseconds , But if we set the pause very low , For example, set it to 20 milliseconds , The most likely result is that the pause target time is too short , As a result, each time the selected collection sets only occupy a small part of the heap memory , The collector's speed of collection gradually cannot keep up with the speed allocated by the distributor , Cause the garbage to pile up slowly . It is likely that the collector will also be able to get some respite time from the free heap memory at first , But the application runs for a long time , Finally, the heap is full Full GC It reduces performance , Therefore, it is usually reasonable to set the expected pause time to one or two or three hundred milliseconds .
G1 Garbage collection and classification
YoungGC
YoungGC It's not about the existing Eden When the area is full, it will trigger immediately ,G1 The accountant calculated the present Eden How long does it take to recycle the area , If the recovery time is much less than the parameter -XX:MaxGCPauseMills Set value , So add the younger generation region, Continue to store new objects , Not immediately YoungGC, Until the next time Eden The area is full ,G1 Calculate the recovery time close to the parameter -XX:MaxGCPauseMills Set value , Then it will trigger Young GC
MixedGC
No FullGC, The old age's heap occupancy reaches the parameter (-XX:InitiatingHeapOccupancyPercent) The set value triggers , Recycle all of Young And part Old( According to expectation GC The pause time is fixed old Priority of district garbage collection ) And the big object area , Normal condition G1 Our garbage collection is to do MixedGC, Mainly using replication algorithm , Need to put each region Copy the surviving objects in the database to another region In go to , If you find Not enough Emptiness of region Being able to host a copy object will trigger once Full GC
Full GC
Stop system program , Then we use a single thread to mark 、 Clean and compress , Good free come out a batch of Region For the next time MixedGC Use , This process is very time consuming .(Shenandoah Optimized for multithreaded collection )
G1 Collector parameter settings
-XX:+UseG1GC: Use G1 The collector
-XX:ParallelGCThreads: Appoint GC Number of threads working
-XX:G1HeapRegionSize: Specify partition size (1MB~32MB, And it must be 2 Of N The next power ), By default, the whole heap is divided into 2048 Zones
-XX:MaxGCPauseMillis: Target pause time ( Default 200ms)
-XX:G1NewSizePercent: New generation memory initial space ( Default whole heap 5%)
-XX:G1MaxNewSizePercent: The largest memory space of the new generation
-XX:TargetSurvivorRatio:Survivor The filling capacity of the zone ( Default 50%),Survivor A group of objects in the area ( Age 1+ Age 2+ Age n The multiple
Age object ) The sum exceeds Survivor Regional 50%, At this time, the age will be n( contain ) All the above objects are put into the elderly generation
-XX:MaxTenuringThreshold: The maximum age threshold ( Default 15)
-XX:InitiatingHeapOccupancyPercent: In the old days, the occupied space reached the whole heap memory threshold ( Default 45%), The new generation and the old generation are mixed
collect (MixedGC), For example, we said that the heap default is 2048 individual region, If there is any approach 1000 individual region It's all old age region, Maybe
It's about to trigger MixedGC 了
-XX:G1MixedGCLiveThresholdPercent( Default 85%) region Only when the number of live objects in is lower than this value will the region, If it exceeds that
It's worth , There are too many survivors , Recycling doesn't mean much .
-XX:G1MixedGCCountTarget: In a recycling process, specify how many times to filter recycling ( Default 8 Time ), In the last screening recovery phase, a
Meeting , Then pause recycling , Restore system operation , Start recycling later , In this way, the system can not be too long for a single pause .
-XX:G1HeapWastePercent( Default 5%): gc The process hollows out region Is the threshold sufficient , When mixing and recycling , Yes Region Recycling all
It is based on the replication algorithm , It's all about recycling Region Put the living objects in the other Region, And then this Region All garbage objects in the are cleared
Get rid of it , In this way, the recycling process will continue to empty out new Region, Once free Region The amount of heap memory 5%, At this point, it will stand
Stop mixing and recycling , It means that the mixed recycling is over .
G1 Garbage collector optimization recommendations
        Suppose the parameters -XX:MaxGCPauseMills The value set is very large , It causes the system to run for a long time , The younger generation may have taken up a lot of heap memory 60% 了 , That's when the younger generation is triggered gc. So there might be a lot of survivors , This leads to Survivor Area can't hold so many objects , It's going to be in the older generation . Or your younger generation gc later , Too many people have survived , Cause to enter Survivor The dynamic age determination rule is triggered after the region , Reached Survivor Regional 50%, It can also quickly lead to some objects entering the older generation .
        So the core here is regulation -XX:MaxGCPauseMills The value of this parameter , To ensure that his younger generation gc Don't be too frequent at the same time , You have to think about every time gc How many people will survive after that , Avoid too many survivors and get into the old generation quickly , Trigger frequently mixed gc.
What scene is suitable for use G1
1. 50% The above heap is occupied by live objects
2. The speed of object allocation and promotion varies greatly
3. Garbage collection takes a long time , exceed 1 second
4. 8GB The above heap memory ( recommended value )
5. The pause time is 500ms within
How to optimize hundreds of thousands of concurrent systems per second JVM
      Kafka Similar systems supporting high concurrency messages are certainly familiar to everyone , about kafka Come on , It's normal to process tens of thousands or even hundreds of thousands of messages per second , In general, deployment kafka Need to use a large memory machine ( such as 64G), In other words, the young generation can be assigned 30 or 40 G Memory is used to support high concurrency , Here comes a question , What we used to say about eden District young gc It's very fast , In this case, will it be implemented quickly ? Obviously , impossible , Because the memory is too large , It will take a lot of time to deal with it , Suppose thirty or forty G Memory recovery may take a few seconds at the fastest , Press kafka This concurrency can be up to thirty or forty G Of eden It's probably just a minute or two , So it means that every minute or two the whole system runs because young gc Carton can't process new messages for a few seconds , Obviously not . So how to optimize this situation , We can use G1 The collector , Set up -XX:MaxGCPauseMills by 50ms, hypothesis 50ms Can recycle three to four G Memory , then 50ms In fact, Carlton can completely accept , Users have almost no perception , Then the whole system can collect garbage while processing business with almost no perception of Caton .
G1 Naturally suitable for this kind of large memory machine JVM function , Can be more perfect to solve the problem of large memory garbage collection time is too long .
ZGC The collector (-XX:+UseZGC)
Reference article :https://wiki.openjdk.java.net/display/zgc/Main
http://cr.openjdk.java.net/~pliden/slides/ZGC-Jfokus-2018.pdf
ZGC Is a JDK 11 A new experimental low latency garbage collector is added in ,ZGC It can be said that it comes from Azul System company-developed C4(Concurrent Continuously Compacting Collector) The collector .

 

ZGC The goal is
As shown in the figure below ,ZGC The main objectives are 4 individual :
  • Support TB The order of magnitude heap . The hard disk of our production environment is not yet available TB Well , That should be enough for the next 10 years , all JAVA The demand of the application has changed Well .
  • Maximum GC The pause time is not more than 10ms. At present, the general online environment is running well JAVA application Minor GC The pause time is 10ms about , Major GC Generally 100ms above (G1 You can adjust the pause time , But if it's too low , It will backfire ), The reason why This is done because its pause time is mainly related to Root Scanning is about , and Root The number has nothing to do with the heap size .
  • Lay the foundation for the future GC Basis of characteristics .
  • In the worst case, throughput will decrease 15%. It's nothing , Pause time is good enough . As for throughput , By expanding capacity, it can be solved in minutes .
      in addition ,Oracle The government mentioned that its biggest advantage is : Its pause time does not increase as the heap grows ! in other words , Dozens of G The pause time of the heap is 10ms following , A few hundred G Even on T The pause time of the heap is also 10ms following .
There are no generations ( temporary )
Single generation , namely ZGC「 There are no generations 」. We know the reason why the previous garbage collectors are divided into generations , Because it comes from “「 Most of the objects live and die 」” False set up , In fact, the object allocation behavior of most systems does conform to this assumption .
So why ZGC There are no generations ? Because it is troublesome to realize generation by generation , The author first implements a relatively simple and available single generation version , It will be optimized in the future .
ZGC Memory layout
ZGC Collector is based on Region Memory layout , There will be no generations for the time being , Used Reading barrier 、 Color pointer And so on - Sorting algorithm's , A garbage collector with low latency as the primary goal .
ZGC Of Region It can be as shown in the figure 3-19 Large as shown 、 in 、 Small three kinds of capacity :
small Region(Small Region) : The capacity is fixed to 2MB, Used to place less than 256KB The little object of .
medium Region(Medium Region) : The capacity is fixed to 32MB, Used to place more than or equal to 256KB But less than 4MB The object of .
large Region(Large Region) : The capacity is not fixed , Can change dynamically , But it has to be 2MB Integer multiple , For placement 4MB Or above . Each large Region in
Only one large object will be stored , It also indicates that although the name is “ large Region”, But it is likely to have a real capacity of less than medium Region, The minimum capacity can be as low as 4MB. large Region stay ZGC Will not be reallocated in the implementation of ( Redistribution is ZGC A kind of processing action of , The collector phase for copying objects , I'll talk about it later ) Of , Because copying a large object is very expensive .

 

 

NUMA-aware
NUMA Corresponding UMA,UMA namely Uniform Memory Access Architecture,NUMA Namely Non Uniform Memory Access Architecture.UMA There is only one block of memory , all CPU To access this memory , Then there will be competition ( Competing for memory bus access ), There's competition, there's lock , Locking efficiency will be affected , and CPU The more cores , The more competition .NUMA Every word CPU There is a block of memory , And there's something on the motherboard that's away from this CPU It's the latest , Every CPU Priority access to this memory , Then the efficiency will be improved :
Server's NUMA Very popular in large-scale systems , It is also a high-performance solution , Especially in terms of system delay .ZGC
It can automatically perceive NUMA Structure and make full use of NUMA Architectural features .

 

Color pointer
Colored Pointers, The color pointer , As shown in the figure below ,ZGC One of the core designs of . The old garbage collector's GC The information is stored in the object header , and ZGC Of GC The information is stored in the pointer .

 

Each object has a 64 Bit pointer , this 64 Bits are divided into :
  • 18 position : Reserved for future use ;
  • 1 position :Finalizable identification , This bit is related to concurrent reference processing , It means that the object can only pass through finalizer Ability to visit ;
  • 1 position :Remapped identification , After setting the value of this bit , Object does not point to relocation set in (relocation set Indicate need GC Of Region aggregate );
  • 1 position :Marked1 identification ;
  • 1 position :Marked0 identification , And the above Marked1 All tag objects are used to assist GC;
  • 42 position : Address of the object ( So it can support 2^42=4T Memory ):
Reading barrier
Previous GC Are all used Write Barrier, This time, ZGC A completely different scheme is adopted , This is ZGC A very important feature .
In the stage of marking and moving objects , Every time 「 Read a pointer from the reference type of the object in the heap 」 When , You need to add a Load Barriers.
So how do we understand it ? Look at the code below , In the first line of code, we try to read an object reference in the heap obj.fieldA And give it to the reference o(fieldA Read barrier will be added only when it is also an object ). If the object is GC It's moved when it's moving , Next JVM There's a reading barrier , This The barrier will update the read pointer to the new address of the object , And put this pointer in the pile “ correct ” To the original field . That's what I mean GC Move objects 了 , The read barrier also finds and corrects pointers , So the application code will always hold the updated valid pointer , And it doesn't need to STW. that ,JVM How to judge whether an object has been moved ? Using the color pointer mentioned above , If the pointer is Bad Color, So the program can't go down yet , need 「slow path」, Fixed pointer ; If the pointer is Good Color, Then the normal downward execution is enough :
ZGC Operation process
ZGC The operation process can be roughly divided into the following four major stages :
  • Concurrent Tags (Concurrent Mark): And G1 equally , Concurrent markup is the stage of traversing object graph for reachability analysis , Its initial mark (Mark Start) And the final mark (Mark End) There will also be short pauses , And G1 The difference is , ZGC The mark of is on the pointer, not on the object On , The marking phase updates the Marked 0、 Marked 1 Sign a .
  • And prepare for reallocation (Concurrent Prepare for Relocate): At this stage, it is necessary to calculate the current revenue according to specific query conditions What to clean up during the set process Region, Will these Region Make up a reallocation set (Relocation Set).ZGC Every time it's recycled, it's scanning everything Region, Save... In exchange for larger scan costs G1 The maintenance cost of the middle memory set .
  • Concurrent reallocation (Concurrent Relocate): Redistribution is ZGC The core stage in the implementation process , In this process, we need to redistribute the centralized storage The live object is copied to the new Region On , And redistribute each of the sets Region Maintain a Forwarding table (Forward Table), Record from old object Steering relationships to new objects .ZGC The collector can know whether an object is in the reallocation set by reference only , If the user thread does not Send access to the object in the reallocation set , This access will be blocked by the preset memory barrier ( Reading barrier ) Intercepted , Then immediately according to Region The forward table record on forwards access to the newly replicated object , At the same time, update the value of the reference , Make it point directly at the new object ,ZGC Call this behavior pointer “ self-healing ”(Self-Healing) Ability .
1 ZGC Because self-healing (SelfHealing) Ability , So only the first time you access an old object, it slows down , Once redistributed, focus on a Region All surviving objects have been copied after ,
2 This Region You can immediately release the allocation for new objects , But the forwarding table has to be kept and can't be released , Because there may be access using this forwarding table .
  • Concurrent remapping (Concurrent Remap): All remapping does is fix all references in the whole heap to old objects in the reassignment set , but yes ZGC Object reference in exists “ self-healing ” function , So this remapping operation is not very urgent .ZGC Very skillfully, what the concurrent remapping phase should do Work , Merge to the next garbage collection cycle in the concurrent marking phase to complete , Anyway, they all need to traverse all objects , In this way, you can merge sections It saves the cost of traversing the object graph once . Once all the pointers have been fixed , The forwarding table that records the relationship between the old and the new objects can be released .

ZGC The problem is

ZGC The biggest problem is Floating garbage .ZGC The pause time is in 10ms following , however ZGC The execution time is still much longer than that . If ZGC The whole process needs to be carried out 10 minute , During this period due to the high rate of object allocation , A lot of new objects will be created , It's hard for these objects to enter the current GC, So next time GC When it's time to recycle , These can only wait until next time GC The object that can be recycled is floating garbage .
Solution
At present, the only way is to increase the capacity of the heap , Make the program get more breathing time , But this is also a solution to the problem . If you need to start from the root Ben can solve this problem , We still need to introduce generational collection , Let the new objects all be created in a special area , And then specifically for this area more frequently 、 Faster collection .

ZGC Parameter setting
Enable ZGC Relatively simple , Set up JVM Parameters can be :-XX:+UnlockExperimentalVMOptions 「
-XX:+UseZGC」. Tuning is not difficult , because ZGC There are not many tuning parameters , not a quarter CMS So complicated . It and G1 equally , There are few parameters that can be tuned , Most of the work JVM It's very automatic . The picture below shows ZGC Tunable parameters :

 

ZGC trigger
ZGC There are 4 The mechanism triggers GC:
  • Timing trigger , The default is not to use , It can be done by ZCollectionInterval Parameter configuration .
  • Warm up trigger , Three times at most , Reach... In heap memory 10%、20%、30% Trigger when , Main time statistics GC Time , For others GC Mechanism use .
  • Allocation rate , Based on normal distribution statistics , Compute memory 99.9% The maximum possible allocation rate , And the time point at which the memory will run out at this rate , Trigger before exhaustion GC( Run out of time - once GC Maximum duration - once GC Detection cycle time ).
  • Active trigger ,( Default on , It can be done by ZProactive Parameter configuration ) From last GC Heap memory growth 10%, Or exceed 5 minutes , Contrast distance Time GC It's the same interval as (49 * once GC The maximum duration of ), More than that triggers .

How to choose a garbage collector
1. Prioritize heap sizing for the server to choose
2. If memory is less than 100M, Using a serial collector
3. If it's a single core , And there is no pause time requirement , Serial or JVM Choose... For yourself
4. If pause time is allowed to exceed 1 second , Choose parallel or JVM Choose by yourself
5. If response time is the most important , And not more than 1 second , Using concurrent collectors
6. 4G You can use parallel,4-8G It can be used ParNew+CMS,8G The above can be used G1, A few hundred G For the above ZGC
Safe points and safe areas
safer It refers to some specific location in the code , When a thread runs to these locations, its state is determined , such JVM You can safely carry out some operations , such as GC etc. , therefore GC It's not triggered immediately when you want to do it , It needs to wait for all threads to run to a safe point before triggering .
The location of these specific safety points are mainly as follows :
  1.   Method before returning
  2.   After calling a method
  3.   Where to throw an exception
  4.   At the end of the loop
The general idea is when garbage collection needs to interrupt the thread , Does not operate directly on threads , Simply set a flag bit , Each thread will take the initiative to poll this flag when executing the process , Once the interrupt flag is found to be true, it will actively interrupt and suspend at the nearest safe point . The location of the polling flag coincides with the security point .
What's a safe area ?
Safe Point It is set for the executing thread .
If a thread is in Sleep Or interrupt state , It can't respond JVM Interrupt request for , Run to Safe Point On .
therefore JVM Introduced Safe Region.Safe Region In a piece of code , The quoting relationship doesn't change . Start anywhere in this area GC It's all safe .

 

原网站

版权声明
本文为[The season when the monsoon dies]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270557284088.html