G1 Garbage Collector Recycling Steps

1: Class loading mechanism:


    Add, test, quasi, solution, initial, make, unload,
    smoke, quasi, sister, initial, wet, shoes

    1.1: Load, convert the class file into a binary stream, load it into the JVM memory and generate a Class object of this class
    1.2: Verify, whether the information contained in the byte stream of the Class file meets the requirements of the current virtual machine
    1.3: Prepare, in the method The memory space used by allocating these class variables in the area
    1.4: Analysis, the virtual machine replaces the symbolic references in the constant pool with direct references
    1.5: Initialization, execution of the class constructor <client> method, the compiler collects the assignment of class variables Operation and statements in the static statement block,
                  the virtual machine ensures that the <client> method of the parent class has been executed, and if there are no static variables and static statements, the client method
                  will not be generated

2: Quote:


    Strong, Soft, Weak, Virtual/Never/Insufficient/After GC/Track Collection Status


3: Class loader:


    Bootstrap class loader, extension class loader, application class loader


4: Parental delegation mechanism:


    Explanation: If you don’t load it yourself, delegate it to the parent class to complete it. If you can’t load it, you can load it yourself.
    Benefits: Using different class loaders will eventually get the same Object object

5:JVM:


    5.1: 1.7-heap, stack, method area, program counter, native method stack
         1.8-heap, stack, metaspace (local memory), program counter, native method stack
             Default heap size: -Xms: 1/64, -Xmx: 1/4
                 Young generation default: -NewRatio: 2, old generation/new generation value = 2
                 New generation to survivor area ratio: 8:1:1
                 New generation size: -Xmn
                 Old generation size: total heap - new generation


6: Tuning tools:

|Instruction line|Jconsole|VisualVM|Jprofiler|Java Flight Recorder|GC Viewer|GC Easy|


6: Identify garbage


    6.1-Reference counting method: Add a reference counter to the object, refer to it +1, and -1 when it fails.
                Advantages: Simple implementation, high judgment efficiency, and no delay in recycling.
                Disadvantage: Requires separate field storage to increase storage overhead.
                      Each assignment adds time overhead.
                      Unable to handle circular references.
    6.2-Accessibility analysis method: Is it reachable from GC Roots to this object? If
                    there is no finalize() method, kill it directly.
                    If there is any, add it to the queue and then finalize().
                Advantages: Solve the problem of circular dependency and prevent memory leaks
        6.2.1-GC Roots:
            6.2.1.1: Parameters and local variables called by each thread
            6.2.1.2: Reference objects in the local method stack
            6.2.1.3: Static attributes in the method area Referenced objects
            6.2.1.4: Objects referenced by constants
            6.2.1.5: Objects held by locks
        6.2.2-finalize method: Process logic before destruction, used for the release of the most resources before object recycling, can only be called once and
                will cause the object to be revived
                Execution time is not guaranteed, and if GC does not occur, there is no chance to execute, which
                seriously affects performance.
        6.2.3-Three states of objects:
                touchable
                , revived
                , untouchable        
    Memory leak: Object is unavailable and garbage collector cannot reclaim
    Memory overflow: Not enough memory, and the garbage collector cannot provide more space

6: Garbage collection algorithm


    6.1-Copy, mark removal, mark finishing
    6.2-Generational recycling idea
        Young generation-copy, age +1, move to old generation when it reaches 15 Old generation
        -marking and finishing
    6.3-Partition recycling idea
        Region


7: Garbage Collector


    7.1-Young generation: Serial (string), ParNew (parallel), Parallel Scavenge (multiple, swallow)-1.8 default,
    7.2-Old generation: Serial Old (string), CMS (initial parallel merge), Parallel Old (parallel) -1.8 Default,
    7.3-Heap: G1 (concurrent)
    7.4-CMS problem:
        7.4.1-Occupying CPU resources and causing the reference program to slow down and the total throughput to drop. The default number of threads is: (number of CPUs + 3) / 4
        7.4.2-CMS collector cannot handle floating garbage
        7.4.3-need to reserve space for program operation during concurrent collection, it will be activated at 68%,
              reserved The space is not enough to throw an exception, enabling Serial Old causes the pause time to be too long.
        7.4.4 - Mark clear, generate fragments.
    G1:
        G1 meets the pause time requirements of the garbage collector as much as possible while achieving high throughput.
        Memory structure: 2048 Regions, each Region size is between 1-32M, and the default young generation initial size is 5% of the whole heap , up to 60% of the entire stack

        PRT: Per Region Table is the reference of RSet to record partitions internally.
            Rare: directly record the card index of the referenced object
            Fine-grained: record the partition
            index of the referenced object Coarse-grained: only record the reference, each partition corresponds to a bit

        SATB:
            Used to record a snapshot of the objects contained in each Region and their survival marker information at a certain point in time.
            
            Write barrier:
        PLAB:
            When a thread allocates memory, it first tries to allocate from PLAB.
            If there is insufficient space in the PLAB, the thread will start a new PLAB.
            This avoids performance bottlenecks caused by multiple threads competing for a global allocation cache.


Understanding of G1 garbage collection:

 The first stage


      1: Object allocation, modify RSet.
      2: Young GC is triggered when the young generation is full.
             2.1: Initial marking,
             2.2: Calculate the Region with the highest recycling value during the concurrent marking and final marking process and put it into CSet 
             2.3: Recycle according to CSet, which is called screening recycling. If most of the large objects are dead, they are also recycled.
             2.4: After recycling, put the living objects into the survivor area, the object age +1, and modify the RSet again
       3: With new space, the user thread can put the new object into the Eden area.


second stage:

After multiple Young GCs, objects in the survivor area whose age exceeds 15 will enter the old age


The third phase:


     At this time, the application thread is still running and continues to generate new objects. PLAB is used to temporarily store the objects promoted from the Eden area. Before performing garbage collection operations, the G1 garbage collector will promote the objects in the PLAB to the old generation in turn, which can reduce the memory allocation operations in the old generation. For each thread, the G1 garbage collector maintains one or more a PLAB. When a thread allocates memory, it first tries to allocate from PLAB. If there is insufficient space in the PLAB, the thread will start a new PLAB. This avoids performance bottlenecks caused by multiple threads competing for a global allocation cache.

The fourth stage:

Before executing Mixed GC, the G1 garbage collector will first record a STAB, which
records the surviving object information and surviving marker information of each Region in the Mixed GC area.
After that, the G1 garbage collector will start from the surviving objects in the Mixed GC area,
check the Region where each surviving object is located, and mark these Regions as "reproducible" candidate areas.
At the same time, various heuristic algorithms are used to select the best Region candidate list,
thereby reducing space fragmentation and improving memory utilization during Mixed GC.

Fifth stage:

1: When the old age storage reaches the threshold, the default is 45%, which will trigger Mixed GC
2: Initial marking.
3: In the process of concurrent marking and final marking, calculate the Region with the largest recovery value (including all Regions) and put it into the CSet.
4: Mixed recovery according to CSet. If a small number of large objects die, a part is also recycled.
5: Copy the surviving objects to other Regions.

Sixth stage:


1: During the copy process, if it is found that there are not enough empty regions to hold the copied object, a Full GC will be triggered.
2: At this point, the execution of the application is completely stopped, and all zone marking and maintenance operations are performed to obtain sufficient space.

scenes to be used:

More than 50% of the heap is occupied by surviving objects.
The speed of object allocation and promotion varies greatly
. Garbage collection takes a very long time. The pause time
of more than 8GB of heap memory (recommended value)
is less than 500ms for more than 1 second.


optimization:


 1: Increase the heap size of the G1 garbage collector: Since the G1 garbage collector uses generational technology, it
       is necessary to increase the heap size appropriately to improve the efficiency of garbage collection. Usually, you can increase the heap size by setting
       the JVM startup parameters -Xmx and -Xms.

2: Set the maximum G1 collection time: When optimizing the G1 garbage collector, you need to know the time required for each garbage collection.
      You can control the maximum garbage collection time by setting the JVM startup parameter -XX:MaxGCPauseMillis,
      thereby improving the efficiency of garbage collection.

3: Adjust the partition size of the G1 garbage collector: The G1 garbage collector divides the heap into several partitions for garbage collection.       The performance of the garbage collector
     can be optimized by adjusting the partition size by setting the JVM startup parameter -XX:G1HeapRegionSize .

4: Increase the number of concurrent threads: The efficiency of the garbage collector can be improved by increasing the number of concurrent threads of the garbage collector.
      You can use the JVM startup parameter -XX:ConcGCThreads to increase the number of concurrent threads,
      thereby improving the concurrent performance of the garbage collector.

5: Avoid using very large objects: The use of the G1 garbage collector should avoid creating very large objects.
      Very large objects can easily lead to problems such as allocation failure and long GC time.

Guess you like

Origin blog.csdn.net/weixin_39519454/article/details/130107022