jvm garbage collector (ZGC)


jvm garbage collector (ZGC)

 

Official website: https://docs.oracle.com/en/java/javase/14/gctuning/z-garbage-collector1.html#GUID-A5A42691-095E-47BA-B6DC-FB4E5FAA43D0

 

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

Introduction to zgc

 

ZGC was introduced in java11, based on the memory layout of r egion, (temporarily) without generations ;

Use read barriers, dye pointers, memory multiple mapping (multiple virtual addresses correspond to a physical address) and other technologies to implement concurrent mark sorting algorithms ;

It is a low-latency garbage collector, which can control the recovery pause time of any size heap memory within 10ms without affecting the throughput .

 

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

Memory layout

 

      

ZGC regions can be created and destroyed dynamically, and the size of regions can be changed dynamically . There are three main categories:

Small region: fixed to 2M, used to store small objects less than 256KB;

Medium region: fixed to 4M, used to store objects greater than or equal to 256KB and less than 4M;

Large region: The size can be changed, and it needs to be an integer multiple of 2M. It stores objects greater than or equal to 4M. The minimum size of a large region is 4M.

 

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

Memory multiple mapping: multiple virtual memories correspond to the same physical memory

 

      

In the current reloate phase of garbage collection, the surviving objects of the region in the redistribution set are copied to other regions ;

After all surviving objects are copied, the region can be reused, and new objects are allocated to the memory address ;

At this time, the reference relationship of the original object may still point to the memory address (the real memory address can be found through the flag bit and the forwarding table), so a many-to-one relationship between the virtual address and the physical address is formed

 

 

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

Dye pointer

 

      

ZGC's concurrent finishing algorithm is implemented by coloring pointers

Mark information is stored on the pointer of the reference object: the upper 18 bits cannot be used, the 4 bits are used to store the mark information, and the lower 42 bits are used to store the object address

The dyed pointer does not support 32-bit platforms , does not support compressed pointers (-XX: + UseCompressedOops) , and can use up to 4TB (2 to the power of 42) of physical memory

 

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

Flag bit description

 

Marked0 / Marked1: Marked bit, whether the marked object is available

Remapped: records whether the object enters the redistribution set (whether the object has moved)

Finalizable: Whether the marked object can only be accessed through fnalize ()

 

Use two marked bits Marked0, Marked1: used alternately in different recycling cycles, the flag bit of the last recycling cycle is invalid in this cycle, reset to 0

For example: Marked0 is used in one cycle, and the live objects are marked as 01 ,

Then the next cycle uses the marked bit Marked1 , and the live object is marked as 10.

 

 

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

Advantage

 

After all the surviving objects in the region are removed, the region can be recovered without having to wait for the reference relationship to be modified before releasing it (the reference relationship modification is maintained through the forwarding table );

The color pointer uses the read barrier to modify the reference relationship , which greatly reduces the number of memory barriers used compared to the write barrier;

Can develop unused 18-bit record mark, relocation and other information , expand the heap memory supported by ZGC from 4TB to 128TB

 

 

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

zgc recycling process

 

      

Concurrent Mark: Concurrent Mark

Before the concurrent mark starts, stop the user thread and enumerate the root node ;

During concurrent marking, users can run , perform reachability analysis and marking based on the root node;

After the concurrent marking ends, stop the user thread and do the final marking ;

 

Concurrent Prepare for relocate: concurrent pre-relocation

Scan the entire heap to find the regions that need to be cleaned, and group these regions into a relocation set (relocation set) ;

The class unloading and weak reference removal that java12 started to support are also completed at this stage

 

Conurrent relocate: concurrent redistribution

Copy the live objects of the region in the redistribution set to other regions, and use the forwarding table to maintain the correspondence between the old and new objects

After all the surviving objects in the region are copied, the memory space of the region can be reused;

The forwarding table cannot be released, and cannot be deleted until the reference relationship of the object in the subsequent stage is modified.

 

Concurrent Remap: Concurrent Remap

According to the forwarding table, modify the memory address pointing to the old object pointer to the memory address of the new object ;

The work of this stage does not have to be completed immediately, the implementation of zgc is to modify the memory address of the pointer in the Concurrent Mark stage of the next stage

 

 

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

Related parameters

 

-XX: + UnlockExperimentalVMOptions: ZGC and java14 are still in the experimental stage, you need to enable this parameter to use

-XX: + UseZGC: use zgc garbage collector

-XX: ConcGCThreads: the number of garbage collection threads used, ZGC will automatically set according to the current cpu

 

 

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

Examples

 

public class Test8 {

    public static void main(String[] args){
        byte[] b=new byte[10];
        b=null;

        System.gc();
    }
}

 

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

Virtual machine parameters

 

-Xms20m -Xmx20m -Xlog:gc* -XX:+UnlockExperimentalVMOptions -XX:+UseZGC

 

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

Console output

 

[0.032s][info][gc,init] Initializing The Z Garbage Collector
[0.032s][info][gc,init] Version: 14+36-1461 (release)
[0.033s][info][gc,init] NUMA Support: Disabled
[0.033s][info][gc,init] CPUs: 8 total, 8 available
[0.033s][info][gc,init] Memory: 8079M
[0.033s][info][gc,init] Large Page Support: Disabled
[0.033s][info][gc,init] Medium Page Size: N/A
[0.033s][info][gc,init] Workers: 1 parallel, 1 concurrent
[0.034s][info][gc,init] Address Space Type: Contiguous/Unrestricted/Complete
[0.034s][info][gc,init] Address Space Size: 320M x 3 = 960M
[0.034s][info][gc,init] Min Capacity: 20M
[0.034s][info][gc,init] Initial Capacity: 20M
[0.034s][info][gc,init] Max Capacity: 20M
[0.034s][info][gc,init] Max Reserve: 2M
[0.034s][info][gc,init] Pre-touch: Disabled
[0.035s][info][gc,init] Uncommit: Disabled
[0.051s][info][gc,init] Runtime Workers: 1 parallel
[0.051s][info][gc     ] Using The Z Garbage Collector
[0.227s][info][gc,start] GC(0) Garbage Collection (Warmup)
[0.228s][info][gc,phases] GC(0) Pause Mark Start 0.558ms
[0.233s][info][gc,phases] GC(0) Concurrent Mark 5.530ms
[0.234s][info][gc,phases] GC(0) Pause Mark End 0.084ms
[0.234s][info][gc,phases] GC(0) Concurrent Process Non-Strong References 0.589ms
[0.234s][info][gc,phases] GC(0) Concurrent Reset Relocation Set 0.001ms
[0.248s][info][gc,phases] GC(0) Concurrent Select Relocation Set 13.698ms
[0.249s][info][gc,phases] GC(0) Pause Relocate Start 0.425ms
[0.253s][info][gc,phases] GC(0) Concurrent Relocate 3.846ms
[0.253s][info][gc,load  ] GC(0) Load: 0.00/0.00/0.00
[0.253s][info][gc,mmu   ] GC(0) MMU: 2ms/72.1%, 5ms/88.8%, 10ms/93.6%, 20ms/96.8%, 50ms/97.9%, 100ms/98.9%
[0.253s][info][gc,marking] GC(0) Mark: 1 stripe(s), 2 proactive flush(es), 1 terminate flush(es), 1 completion(s), 0 continuation(s) 
[0.253s][info][gc,reloc  ] GC(0) Relocation: Successful, 1M relocated
[0.253s][info][gc,nmethod] GC(0) NMethods: 178 registered, 0 unregistered
[0.253s][info][gc,metaspace] GC(0) Metaspace: 5M used, 5M capacity, 5M committed, 8M reserved
[0.253s][info][gc,ref      ] GC(0) Soft: 36 encountered, 0 discovered, 0 enqueued
[0.253s][info][gc,ref      ] GC(0) Weak: 99 encountered, 92 discovered, 17 enqueued
[0.253s][info][gc,ref      ] GC(0) Final: 0 encountered, 0 discovered, 0 enqueued
[0.253s][info][gc,ref      ] GC(0) Phantom: 14 encountered, 14 discovered, 13 enqueued
[0.253s][info][gc,heap     ] GC(0) Min Capacity: 20M(100%)
[0.253s][info][gc,heap     ] GC(0) Max Capacity: 20M(100%)
[0.253s][info][gc,heap     ] GC(0) Soft Max Capacity: 20M(100%)
[0.253s][info][gc,heap     ] GC(0)                Mark Start          Mark End        Relocate Start      Relocate End           High               Low         
[0.253s][info][gc,heap     ] GC(0)  Capacity:       20M (100%)         20M (100%)         20M (100%)         20M (100%)         20M (100%)         20M (100%)   
[0.253s][info][gc,heap     ] GC(0)   Reserve:        2M (10%)           2M (10%)           2M (10%)           2M (10%)           2M (10%)           2M (10%)    
[0.253s][info][gc,heap     ] GC(0)      Free:       14M (70%)          12M (60%)          12M (60%)          14M (70%)          14M (70%)          10M (50%)    
[0.253s][info][gc,heap     ] GC(0)      Used:        4M (20%)           6M (30%)           6M (30%)           4M (20%)           8M (40%)           4M (20%)    
[0.253s][info][gc,heap     ] GC(0)      Live:         -                 1M (6%)            1M (6%)            1M (6%)             -                  -          
[0.253s][info][gc,heap     ] GC(0) Allocated:         -                 2M (10%)           2M (10%)           4M (20%)            -                  -          
[0.254s][info][gc,heap     ] GC(0)   Garbage:         -                 2M (14%)           2M (14%)           0M (4%)             -                  -          
[0.254s][info][gc,heap     ] GC(0) Reclaimed:         -                  -                 0M (0%)            2M (10%)            -                  -          
[0.254s][info][gc          ] GC(0) Garbage Collection (Warmup) 4M(20%)->4M(20%)
[0.294s][info][gc,start    ] GC(1) Garbage Collection (System.gc())
[0.295s][info][gc,phases   ] GC(1) Pause Mark Start 0.491ms
[0.302s][info][gc,phases   ] GC(1) Concurrent Mark 6.725ms
[0.302s][info][gc,phases   ] GC(1) Pause Mark End 0.036ms
[0.303s][info][gc,phases   ] GC(1) Concurrent Process Non-Strong References 0.723ms
[0.303s][info][gc,phases   ] GC(1) Concurrent Reset Relocation Set 0.003ms
[0.305s][info][gc,phases   ] GC(1) Concurrent Select Relocation Set 2.693ms
[0.306s][info][gc,phases   ] GC(1) Pause Relocate Start 0.347ms
[0.310s][info][gc,phases   ] GC(1) Concurrent Relocate 3.839ms
[0.310s][info][gc,load     ] GC(1) Load: 0.00/0.00/0.00
[0.310s][info][gc,mmu      ] GC(1) MMU: 2ms/72.1%, 5ms/88.8%, 10ms/93.6%, 20ms/95.6%, 50ms/97.9%, 100ms/98.1%
[0.310s][info][gc,marking  ] GC(1) Mark: 1 stripe(s), 2 proactive flush(es), 1 terminate flush(es), 0 completion(s), 0 continuation(s) 
[0.310s][info][gc,reloc    ] GC(1) Relocation: Successful, 1M relocated
[0.310s][info][gc,nmethod  ] GC(1) NMethods: 199 registered, 0 unregistered
[0.310s][info][gc,metaspace] GC(1) Metaspace: 6M used, 6M capacity, 6M committed, 8M reserved
[0.310s][info][gc,ref      ] GC(1) Soft: 40 encountered, 0 discovered, 0 enqueued
[0.310s][info][gc,ref      ] GC(1) Weak: 89 encountered, 78 discovered, 1 enqueued
[0.310s][info][gc,ref      ] GC(1) Final: 0 encountered, 0 discovered, 0 enqueued
[0.310s][info][gc,ref      ] GC(1) Phantom: 5 encountered, 3 discovered, 0 enqueued
[0.310s][info][gc,heap     ] GC(1) Min Capacity: 20M(100%)
[0.310s][info][gc,heap     ] GC(1) Max Capacity: 20M(100%)
[0.310s][info][gc,heap     ] GC(1) Soft Max Capacity: 20M(100%)
[0.310s][info][gc,heap     ] GC(1)                Mark Start          Mark End        Relocate Start      Relocate End           High               Low         
[0.310s][info][gc,heap     ] GC(1)  Capacity:       20M (100%)         20M (100%)         20M (100%)         20M (100%)         20M (100%)         20M (100%)   
[0.310s][info][gc,heap     ] GC(1)   Reserve:        2M (10%)           2M (10%)           2M (10%)           2M (10%)           2M (10%)           2M (10%)    
[0.310s][info][gc,heap     ] GC(1)      Free:       14M (70%)          12M (60%)          12M (60%)          14M (70%)          14M (70%)          10M (50%)    
[0.310s][info][gc,heap     ] GC(1)      Used:        4M (20%)           6M (30%)           6M (30%)           4M (20%)           8M (40%)           4M (20%)    
[0.310s][info][gc,heap     ] GC(1)      Live:         -                 1M (6%)            1M (6%)            1M (6%)             -                  -          
[0.310s][info][gc,heap     ] GC(1) Allocated:         -                 2M (10%)           2M (10%)           4M (20%)            -                  -          
[0.310s][info][gc,heap     ] GC(1)   Garbage:         -                 2M (14%)           2M (14%)           0M (4%)             -                  -          
[0.310s][info][gc,heap     ] GC(1) Reclaimed:         -                  -                 0M (0%)            2M (10%)            -                  -          
[0.310s][info][gc          ] GC(1) Garbage Collection (System.gc()) 4M(20%)->4M(20%)
[0.313s][info][gc,heap,exit] Heap
[0.313s][info][gc,heap,exit]  ZHeap           used 4M, capacity 20M, max capacity 20M
[0.313s][info][gc,heap,exit]  Metaspace       used 6415K, capacity 6463K, committed 6656K, reserved 8192K

 

 

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

Guess you like

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