当前位置:网站首页>Garbage first of JVM garbage collector

Garbage first of JVM garbage collector

2022-07-06 17:16:00 TiggerRun

Garbage collector

Note navigation

Garbage First GC

Record JVM Learning notes , Understanding may be biased , Hope to point out .

G1 Garbage collector

key word :Region、 Partition algorithm 、 The delay is controllable 、 Pause prediction model 、 parallel 、 Concurrent 、RSet、MixedGC

G1 What we hope to achieve : Improve throughput as much as possible with controllable delay

summary

  • G1 It's a parallel 、 Concurrent Dual garbage collector , Divide the memory into many irrelevant areas (Region Physically discontinuous )
  • G1 Avoid the whole heap in a planned way GC, Each time according to the allowed collection time , Give priority to collecting the most valuable Region
  • G1 Facing the server application , It is mainly equipped with Multicore CPU And large memory machines
  • G1 stay JDK7 Officially enabled in ,JDK9 The default garbage collector after

Memory allocation policy

stay G1 Two memory allocation strategies are used in the garbage collector at the same time

  1. Pointer collision : In a Region Allocate memory for new objects through pointer collision
  2. Free list : Maintain a LinkedList To save free Region

Keep the idea of generational collection

  • G1 Still keep the idea of generations , Or divide the heap memory into The new generation Old age
  • The memory distribution is no longer fixed ,G1 Divide memory into multiple Region
  • Region Sure dynamic Play different roles ( The new generation Old age Humongous), More than one size Region Objects of will be stored in continuous HumongousRegion in , Usually, it will be treated as the elderly generation .

 Insert picture description here

Spatial integration

  • G1 There is defragmentation ,G1 Memory reclamation of Region In units of ,Region Between Copy algorithm , On the whole, it can be regarded as a label compression algorithm .

Pause prediction statistical model

G1 adopt The mean attenuation For each Region Time consumption of recycling 、 Calculate the amount of garbage and other measurable costs to obtain a series of statistical values . The mean value of attenuation can be expressed Current Average state , Use statistics to predict that garbage collection will start now , Which one Region There will be better benefits . Maintain a Priority list , So it's called Garbage First Recycling benefits first Region.

Solutions for cross region references

 Insert picture description here

Card table

Card table (CardTable), By memory card (Card) constitute

every last Region The default in accordance with the 512kb It is divided into many cards to form a card table ,RSet It records the location of the card, not the object itself

Memory set

Memory set (Remember Set,R Set), Is to solve cross Region Object reference of , stay YoungGC when , If an object is referenced by an older object , Should not be cleared , But because of G1 The heap memory of the collector is partitioned , You need to traverse the whole heap to get the reference relationship of the object , stay YGC It is unreasonable to traverse the entire heap memory .

every last Region All assigned a RSet, Stored the reference relationship of the object (Region1 Of Region1Card_x Refer to the Region2 Of Region2Card_y), After having such a reference relationship , Reachability analysis only needs to obtain GC Roots, There is no need to traverse the entire heap space

Detailed explanation of recycling stage

YOUNG GC Stage

  1. Root scan ( Initialization tag )
  2. Update dirty card queue ( Maybe the reference relationship of the card was modified during the code execution ), to update Rset( Sync ), This ensures what you get Rset The reference relationship of is up-to-date
  3. Handle Rset, Identify those cited by older generations Eden District 、S Objects in the zone , That is, tell the garbage collector that this object is not garbage
  4. copy object ,Eden Objects that live in the area will be copied to S Free area of zone , Age goes up , Those who reach the threshold enter the elderly area , Some objects will be promoted to the elderly area ( Because it's a replication algorithm ,G1 Realize defragmentation )
  5. Handling references , Caused by copying objects Region change , So update Rset

Concurrent tagging phase

The process of marking garbage is time-consuming, because it needs to traverse the reference relationship , But this process is executed concurrently , Not at all STW,GC Threads and user threads work at the same time .

  1. Concurrent Tags : If you find that Region It's all garbage and will be carried out directly Real time recycling Instead of waiting for the next stage .
  2. Mark again (STW): Because the process of concurrent marking is concurrent , The user thread may have modified the reference relationship , Therefore, the marking needs to be corrected .
  3. Exclusive cleanup (STW): Calculate each Region Recovery ratio of , And sort it out , Prepare for the next phase
  4. Concurrent cleanup : Identify and completely clean up idle Region

MIXED GC Stage

  • In this stage , rate of recovery 100% Of Region It has been cleared in real time , Only some garbage is left Region
  • When the heap memory usage reaches a certain threshold -XX:G1MixedGCLiveThresholdPercent after , Automatic mixing and recycling
  • distinguish MixedGC and FullGC,MixedGC Only part of the old age is recycled
  • Old garbage memory segmentation will be divided 8 Time -XX:G1MixedGCCountTarget To recycle , Those with high recycling ratio will be recycled first
  • Of course, it is not necessarily implemented 8 Time , because -XX:G1HeapWastePercent Specifies the allowable waste space ratio

G1 The defects that exist

  • Extra memory space
  • Additional execution load

G1 The applicable scenarios of

  • The first is the server application 、 Large memory 、 many CPU processor
  • Application requirements GC It's low latency , The pause time is controllable
  • Can be substituted for CMS

Be careful

  • The pause time is not as short as possible , If the pause is too short , It may result in only a few being processed at a time Region, Object generation is faster than garbage disposal , It will eventually lead to FullGC

Common parameters

Parameter name The default value is effect
-XX:+UseG1GCJDK9 Later versions use G1 start-up G1 Garbage collector
-XX:G1HeapRegionSize1m Set up Region Size (1m~32m Must be 2 The power of )
-XX:MaxGCPauseMillis200ms Set the maximum pause time
-XX:ParallelGCThreads8 Number of parallel threads
-XX:ConcGCThreads2 Number of concurrent threads , recommend 1/4 Parallel thread
-XX:InitiatingHeapOccupancyPercent45% Set the memory usage threshold that triggers the concurrency flag
-XX:G1MixedGCCountTarget8MixedGC The stage is completed in several times
-XX:G1MixedGCLiveThresholdPercent65% Set trigger MixedGC Heap memory usage threshold
-XX:G1HeapWastePercent10% Set the allowable heap memory waste

Optimization Suggestions

  1. Young generation size
    Use G1 Garbage collector , Don't pass Xmn-XX:NewRatio Set a fixed younger generation size , It is to hand over to G1 Garbage collector dynamic settings , Otherwise, the target pause time will be overwritten .

  2. The pause time should not be set too small

原网站

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