jvm garbage collector (G1)


jvm garbage collector (G1)

 

***************************

g1 Introduction

 

      

The g1 garbage collector performs garbage collection on the entire heap memory, divides the heap memory into multiple regions, retains the concept of the new generation and the old generation, and has the following characteristics:

Parallelism: During garbage collection, there can be multiple garbage threads running at the same time, improving the efficiency of collection

Concurrency: During garbage collection, it can work simultaneously with user threads

Space sorting: During g1 garbage collection, objects will be moved to avoid space debris

Predictability: g1 will preferentially reclaim regions with larger memory, and will not reclaim all regions, reducing garbage collection pause time

 

 

***************************

Garbage collection process: new generation garbage collection, concurrent marking cycle, mixed collection, full gc

 

********************

New generation garbage collection

 

      

The new generation of garbage collection mainly recycles eden and survivor;

Before and after garbage collection, the eden area is emptied , and part of the space will also be recovered in the survivor area ;

Since the new generation of objects may enter the elderly area, the elderly area may increase

 

********************

Concurrent mark cycle

 

      

Initial marking: Mark objects directly reachable from the root node, before the initial marking will trigger a new generation of garbage collection, the user thread stops running during the initial marking

Root area scan: Mark the old generation objects that are directly reachable in the survivor area. Since the young generation gc will change the survivor, the young generation gc stops at this stage, but the user thread can work normally

Concurrent marking: scan the entire heap of surviving objects and mark them. At this time, young generation gc can be performed, and user threads can work normally.

Re-marking: Because the user thread can run during the concurrent marking phase, re-marking modifies the marking result, creates a snapshot before re-marking, and speeds up the re-marking process, at which time the user thread stops

Exclusive pre-cleaning: sort and mark each area according to the size of the recyclable space for recycling in the mixed recovery phase, at which time the user thread stops

Concurrent cleanup: Recover areas where there are no surviving objects (if there are surviving objects, they are not collected, most of them are collected in the mixed recovery phase)

 

      

eden area: The young generation gc is performed before the initial mark, eden is emptied, because the user thread can work during the recovery, after the concurrent mark cycle, the eden area will still be used;

Area G: The area recovered in the mixed recovery phase

 

********************

Mixed recovery stage

 

      

The concurrent marking cycle will reclaim part of the space, and most of the garbage collection work will be carried out in the mixed collection stage;

Mixed recycling does not necessarily recycle all the areas marked as g, only the few areas with the largest memory can be recycled first

Living objects will move to other spaces, reducing space debris

 

            

Mixed recycling will be performed multiple times until enough memory space is reclaimed, and then trigger a new young generation gc, concurrent marking cycle

 

********************

full gc

 

Trigger condition: if the new generation is recycled, the survivor and the old generation cannot store the surviving objects; the mixed recycling stage cannot reclaim sufficient memory space

 

 

***************************

Related parameters

 

-XX: + UseG1GC: use g1 garbage collector

-XX: MaxGCPauseMillis = value: the maximum time for gc to pause

-XX: ParallelGCThreads = value: number of garbage collection threads

-XX: InitiatingHeapOccupancyPercent = value: When the heap usage reaches the set value, the concurrent marking cycle is triggered. If the value is too large, it will cause full gc, and if it is too small, the concurrent marking cycle will be triggered frequently. The default value is 45

 

 

Published 387 original articles · Like 98 · Visits 30,000+

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/105090300