当前位置:网站首页>12 pictures +6k figure ZGC garbage collector and tuning skills

12 pictures +6k figure ZGC garbage collector and tuning skills

2022-07-23 17:25:00 InfoQ

Introduction

Let's talk today  ZGC.ZGC(Z Garbage Collector)  Is a performance ratio  G1  Better garbage collector .ZGC  The first time it appeared was in  JDK 11  Introduce... With experimental characteristics , This is also  JDK 11  The biggest bright spot in . stay  JDK 15  in  ZGC  It's no longer an experimental function , It can be officially put into production , Use  –XX:+UseZGC  Enable  ZGC.

Text

ZGC  Yes  3  There are two important characteristics :

The pause time will not exceed  10 ms.
JDK 16  After the release of ,GC  The pause time has been reduced to  1 ms  within , And the time complexity is  o(1), That means  GC  The pause time is a fixed value , It is not affected by the size of heap memory .
The picture below is from :https://malloc.se/blog/zgc-jdk16
null
  • The biggest support  16TB  A lot of , Minimum support  8MB  A small pile of .
  • Follow  G1  comparison , The impact on application throughput is less than  15 %.

1、 Memory multiple mapping

Memory multiple mapping , Is the use of  mmap  Map different virtual memory addresses to the same physical memory address . Here's the picture :
null
ZGC  In order to manage memory more flexibly and efficiently , Memory multi mapping used , Map the same physical memory to  Marked0、Marked1  and  Remapped  Three virtual memories .
When an application creates an object , Will apply for a virtual address on the heap , At this time  ZGC  Will be in... For this object  Marked0、Marked1  and  Remapped  These three view spaces apply for a virtual address respectively , These three virtual addresses map to the same physical address .
Marked0、Marked1  and  Remapped  These three virtual memories are used as  ZGC  Three view spaces of , There can only be one valid... At the same time point .ZGC  It is through the switching of these three view spaces , To complete concurrent garbage collection .

Related video analysis :
JVM Tuning practice

2、 Dye pointer

2.1  Three color mark review
We know  G1  The garbage collector uses three color markers , Here's a review . The following is an example of object reference in the three color marking process :
null
There are three colors in total , The explanation is as follows : white : This object has not been accessed by the marked thread . gray : This object has been accessed , However, the other objects referenced by this object have not been fully accessed . black : This object has been accessed , And other objects referenced by this object have also been accessed .
The process of three color marking is as follows : Initial stage , All objects are white . take  GC Roots  Directly referenced objects are marked gray . Handle gray objects , Turns all objects referenced by the current gray object gray , Then change the current gray object to black . Repeat step  3, Until there are no gray objects . After the three color mark , White objects are objects that are not referenced ( Like the one above  H  and  G), Can be recycled .

2.2  Dye pointer

ZGC  Before appearance , GC  The information is stored in the header of the object  Mark Word  in . such as  64  Bit  JVM, The head of the object  Mark Word  The information saved in is shown in the figure below :
null
front  62 Bit saved  GC  Information , The last two bits hold the lock flag .
ZGC  One of my great innovations is to  GC  The information is saved on the dyeing pointer . Coloring a pointer is a technique to store a small amount of information directly on the pointer . stay  64  position  JVM  in , The object pointer is  64  position , Here's the picture :
null
In this  64  Bit pointer , high  16  All places are  0, Not for the time being . The rest  48  Bit supported memory can reach  256 TB(2 ^48), This can meet the needs of most large servers . however  ZGC  Not yet  48  Bits are used to store object information , But with high  4  Bit holds four flag bits , such  ZGC  The maximum memory that can be managed can reach  16 TB(2 ^ 44).
Through these four flag bits ,JVM  You can directly see the three color mark status of the object from the pointer (Marked0、Marked1)、 Whether the reallocation set is entered (Remapped)、 Whether it is necessary to pass  finalize  Method to access (Finalizable).
No object access is required to get  GC  Information , This greatly improves  GC  efficiency .

3、 Memory layout

Let's first review  G1  The memory layout of the garbage collector .G1 Divide the whole heap into the same size  region, Each heap can have about  2048  individual region, Every  region  The size is  1~32 MB ( Must be  2  To the power of ). Here's the picture :
null
Follow  G1  similar ,ZGC  Heap memory is also based on  Region  To distribute , however  ZGC  It does not distinguish between the new generation and the old . The difference is ,ZGC  Of  Region  Support dynamic creation and destruction , also  Region  The size of is not fixed , There are three types of  Region :
  • Small Region:2MB, It is mainly used to place less than  256 KB  The little object of .
  • Medium Region:32MB, It is mainly used to place more than or equal to  256 KB  Less than  4 MB  The object of .
  • Large Region:N * 2MB. This type of  Region  It can change dynamically , But it has to be  2MB  Integer multiple , Minimum support  4 MB. Every  Large Region  Place only one large object , And will not be redistributed .

4、 Reading barrier

The read barrier is similar to  Spring AOP  Pre enhancement of , yes  JVM  Insert a small piece of code into the application code , When the application thread reads the reference of the object from the heap , Will execute this code first . Be careful : Only when reading a reference to an object from heap memory , Will execute this code . Only the first line of the following code needs to add a read barrier .
Object o = obj.FieldA
Object p = o // Instead of reading references from the heap
o.dosomething() // Instead of reading references from the heap
int i = obj.FieldB // It's not a reference type
The read barrier passes through when interpreting execution  load  Relevant bytecode instructions load data . The function is in the process of object marking and transfer , Judge whether the reference address of the object meets the conditions , And act accordingly . Here's the picture :
null
Mark 、 See the next section for the process of transferring and relocating . The read barrier will have an impact on the performance of the application , According to the test , The highest impact on performance is  4%, But it improves  GC  Concurrency capability , To reduce the  STW.

5 GC  The process

As I said before ,ZGC  Using memory multiple mapping technology , Map physical memory to  Marked0、Marked1  and  Remapped  Three address views , Using the switching of address view ,ZGC  It realizes efficient concurrent collection .
ZGC  The garbage collection process includes marking 、 There are three stages of transfer and relocation . Here's the picture :
null
ZGC  After the initialization , The address view of the entire memory space is set to  Remapped.

5.1  Initial marker

from  GC Roots  set out , find  GC Roots  Directly referenced objects , Put into the active object collection , This process requires  STW, however  STW  Time with  GC Roots  The quantity is proportional to , It takes less time .

5.2  Concurrent Tags

During concurrent tagging ,GC  Threads and  Java  Application threads run in parallel . This process needs to pay attention to the following points :
  • GC  When the tag thread accesses an object , If the object address view is  Remapped, Just switch the object address view to  Marked0, If the object address view is already  Marked0, Indicates that it has been accessed by other tag threads , Skip do not process .
  • In the process of marking Java  The newly created object of the application thread will directly enter  Marked0  View .
  • In the process of marking Java  When an application thread accesses an object , If the address view of the object is  Remapped, Just switch the object address view to  Marked0, You can refer to the reading barrier mentioned earlier .
  • After marking , If the object address view is  Marked0, That's active , If the object address view is  Remapped, That's inactive .
The active view of the marking phase may also be  Marked1, Why two views ?
Two views are used here to distinguish between the previous tag and this tag . If the marked view is  Marked0, Then the next time the concurrency tag switches the view to  Marked1. This can cooperate with  ZGC  According to the practice of recycling garbage on page . Here's the picture :
null
At the second mark , If you still switch to  Marked0, that  2  This object cannot tell whether it is active or last marked . If the second mark is switched to  Marked1, You can distinguish .
At this time  Marked0  The object of this view is that the last marking process was marked as too active , It was not transferred when it was transferred , But this time the tag is not marked as an active object .Marked1  The object of the view is the object marked as active this time .Remapped  The object of the view is that the garbage collection was transferred or  Java  The application thread has accessed , Objects marked as inactive in this garbage collection .

5.3  Re labelled

Concurrent tagging phase  GC  Threads and  Java  Application threads execute concurrently , During the marking process, there may be missing marking caused by the change of reference relationship . Relabeling phase relabels objects that have changed in the concurrent marking phase , There are also non strong references ( Soft application , Virtual reference, etc ) Parallel marking .
This stage requires  STW, But there are fewer objects to mark , It takes a short time .

5.4  Initial transfer

Transfer is to copy the active object to the new memory , The previous memory space can be recycled .
Initial transfer requires scanning  GC Roots  Directly reference the object and transfer it , This process requires  STW,STW  Time follows  GC Roots  In direct proportion to .

5.5  Concurrent transfers

Concurrent transfer process  GC  Threads and  Java  Threads are concurrent . It's already said , During the transfer, the object view will be cut back  Remapped .
The following points need to be paid attention to in the transfer process :
  • If  GC  The view of the thread access object is  Marked0, Then transfer the object , And set the object view to  Remapped.
  • If  GC  The view of the thread access object is  Remapped, The description is used by other  GC  Thread processed , Skip no more processing .
  • During concurrent transfer  Java  The new object address view created by the application thread is  Remapped.
  • If  Java  The object accessed by the application thread is marked as active and the object view is  Marked0, Then transfer the object , And set the object view to  Remapped.

5.6  relocation

The address of the transfer process object has changed , At this stage , Adjust all pointers to the old address of the object to the new address of the object .

6、 Garbage collection algorithm

ZGC  Using tag  -  Sorting algorithm , The idea of the algorithm is to move all living objects to one side of the heap , After moving, recycle the objects outside the boundary .

6.1 JDK 16  Before

stay  JDK 16  Before ,ZGC  Will reserve (Reserve) Heap memory together , This reserved memory cannot be used for  Java  Memory allocation for threads . Even from  Java  From the perspective of threads, the heap memory is full and cannot be used  Reserve, Only  GC  It can only be used when moving living objects in the process . Here's the picture :
null
The advantage of this is that the algorithm is simple , Very suitable for parallel collection . But there are several problems with this :
  • Because there is reserved memory , Can give  Java  The heap memory allocated by the thread is less than  JVM  Declared heap memory .
  • Reserve  For storage only  GC  Objects moved in the process , A little memory waste .
  • because  Reserve  You can't give  GC  Moving objects in the process  Java  Thread usage , The moving thread may not be able to complete the object moving because it cannot apply for enough memory , This will cause the application to return again  OOM.

6.2 JDK 16  improvement

JDK 16  After the release of ,ZGC  Supports moving objects in place (G1  stay  Full GC  It is also moved on the spot when ). The advantage of this is that there is no need to reserve free memory . Here's the picture :
null
However, there are some challenges in moving locally . such as : The order of moving objects must be considered , Otherwise, objects that have not been moved may be overwritten . This requires  GC  Better collaboration between threads , It is not conducive to concurrent collection , At the same time, it will also lead to the of moving objects  Java  Threads need to consider what they can do and what they can't do .
In order to get better  GC  performance ,JDK 16  While supporting local relocation , Reservation is also supported (Reserve) The way heap memory works , also  ZGC  There is no need to really reserve free heap memory . By default , As long as there is free  region,ZGC  Will use the method of reserving heap memory , If there is no free  region, otherwise  ZGC  Local movement will be enabled . If you have free time  region, ZGC  It will switch to the move mode of reserved heap memory .

7、 summary

Memory multiple mapping and the introduction of colored pointers , send  ZGC  Greatly improve the performance of concurrency .
ZGC  Only  3  A need  STW  The stage of , The initial marking and initial transfer only need to scan all  GC Roots,STW  Time  GC Roots  In proportion to the number of , It won't take much time . The relabeling process mainly deals with the object whose reference address changes during concurrent marking , The number of these objects is relatively small , It takes a very short time . You can see the whole thing  ZGC  Of  STW  Time is almost only related to  GC Roots  Quantity matters , Does not change with heap size and number of objects .
ZGC  There is also a drawback , Floating garbage . because  ZGC  There is no generational concept , although  ZGC  Of  STW  Time in  1ms  within , however  ZGC  The whole execution process of is still very time-consuming . In the process  Java  Threads may create a large number of new objects , These objects become floating garbage , I can only wait for next time  GC  When it's time to recycle .
Link to the original text :https://mp.weixin.qq.com/s/UuelBa8vasVbDjnUzlIs4w
原网站

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