JVM Tuning Summary (6) - A New Generation of Garbage Collection Algorithms

Garbage Collection Bottleneck

    The traditional generational garbage collection method has minimized the burden of garbage collection on the application to a certain extent, and pushed the throughput of the application to a limit. But one problem he can't solve is the application pause caused by Full GC. In some application scenarios with high real-time requirements, the accumulation of requests and request failures caused by GC pauses are unacceptable. This type of application may require the return time of the request to be within hundreds or even tens of milliseconds. If the generational garbage collection method is to achieve this target, the maximum heap setting can only be limited to a relatively small range, but this has limitations. The processing power of the application itself is also unacceptable.

    The generational garbage collection method does also consider the real-time requirements and provides a concurrent collector, which supports the setting of the maximum pause time, but is limited by the memory division model of the generational garbage collection, and its effect is not very satisfactory.

    In order to meet the real-time requirements (in fact, the original design of the Java language is also on embedded systems), a new garbage collection method is ready to emerge, which supports both short pause time and large memory space allocation. It can solve the problems brought by the traditional generation method very well.

The evolution of incremental collection

    The incremental collection method can theoretically solve the problems brought by the traditional generation method. Incremental collection divides the heap space into a series of memory blocks. When using, use a part of them first (not all of them), and put the surviving objects in the previously used part into the unused space during garbage collection. , in this way, the effect of collecting while being used can be realized, avoiding the situation of suspending the recycling after the entire use of the traditional generation method.

    Of course, the traditional generational collection method also provides concurrent collection, but he has a very fatal place, that is, the entire heap is used as a memory block, which on the one hand will cause fragmentation (cannot be compressed), on the other hand, his every time The collection is the collection of the entire heap, which cannot be selected, and the control of the pause time is still very weak. The incremental method, through the block of memory space, can solve the above problem.

Garbage Firest(G1)

The content of this part is mainly referred to here . This article is an interpretation of the G1 algorithm paper. I didn't add anything.

Target

From the design goal, G1 is completely prepared for large-scale applications.

support for very large heaps

high throughput

  -- Supports multiple CPUs and garbage collection threads

  -- use parallel collection in case the main thread is suspended

  -- In the case of the main thread running, use concurrent collection

Real-time target: Configurable to only take up to M milliseconds in N milliseconds for garbage collection

Of course, G1 needs to meet the real-time requirements. Compared with the traditional generational recycling algorithm, there will be some performance losses.

Detailed Algorithm

    G1 can be said to draw on the strengths of many families, and strive to achieve a perfection. He absorbs the advantages of incremental collection and divides the entire heap into regions of equal size. The reclamation and division of memory are based on regions; at the same time, he also absorbed the characteristics of CMS, divided the garbage collection process into several stages, and dispersed a garbage collection process; moreover, G1 also agreed with the idea of ​​generational garbage collection, It is considered that different objects have different life cycles and can be collected in different ways. Therefore, it also supports generational garbage collection. In order to achieve the predictability of the recovery time, after G1 scans the region, it sorts the size of the active objects in it, and first collects those regions with small active objects in order to quickly reclaim the space (fewer active objects to be copied) , because the active objects are small, most of them can be considered garbage, so this method is called Garbage First (G1) garbage collection algorithm, that is: garbage-first collection.

 

Recycling steps:

Initial Marking

    G1 saves two bitmaps for identification for each region, one is the previous marking bitmap, the other is the next marking bitmap, and the bitmap contains a bit of address information to point to the starting point of the object.

    Before starting Initial Marking, firstly clear the next marking bitmap concurrently, then stop all application threads, and scan to identify the objects that can be directly accessed by root in each region, and put the top value in the region into next top at mark start (TAMS ), after which all application threads are resumed.

    The conditions that trigger the execution of this step are:

    G1 defines a threshold value of the percentage of JVM Heap size, called h, and there is another H, the value of H is (1-h)*Heap Size. At present, the value of h is fixed, and subsequent G1 may It is changed to dynamic, and it is dynamically adjusted according to the operation of the jvm. In the generational mode, G1 also defines a u and a soft limit. The value of the soft limit is Hu*Heap Size. When the memory used in Heap exceeds When the soft limit value is set, this step will be executed as soon as possible within the GC pause time allowed by the application after a clean up is executed;

    In pure mode, G1 forms a ring with marking and clean up, so that clean up can fully use the marking information. When clean up starts to recycle, the regions that can bring the most memory space are reclaimed first. up, when the regions with little space are recovered, G1 reinitializes a new ring of marking and clean up.

 

Concurrent Marking

    Traverse the objects scanned by the previous Initial Marking to identify the active state of the underlying objects of these objects, record the relationship between the objects concurrently modified by the application thread during this period in the remembered set logs, and put the newly created objects in the In the address range higher than the top value, the default state of these newly created objects is active, and the top value is modified at the same time.

Final Marking Pause

    When the remembered set logs of the application thread are not full, it will not be put into the filled RS buffers. In this case, the modification of the card recorded in the remembered set logs will be updated, so this step is required. The first step is to process the contents of the remembered set logs existing in the application thread, and modify the remembered sets accordingly. In this step, the application needs to be suspended and run in parallel.

Live Data Counting and Cleanup

    It is worth noting that in G1, it does not mean that after the Final Marking Pause is executed, the Cleanup step must be executed. Since this step needs to suspend the application, in order to meet the quasi-real-time requirements, G1 needs to specify the largest GC according to the user. The pause time caused to reasonably plan when to execute Cleanup, and there are several situations that will trigger the execution of this step:

    G1 uses the copy method to collect, and must ensure that the space for each "to space" is sufficient. Therefore, the strategy adopted by G1 is to execute the Cleanup step when the used memory space reaches H;

    For G1 in full-young and partially-young generational modes, there are situations that trigger the execution of Cleanup. In full-young mode, G1 is based on the acceptable pause time of the application and the time it takes to recycle young regions. Estimate the number of young regions. When the number of young regions where objects are allocated in the JVM reaches this value, Cleanup will be executed; in partially-young mode, it will be executed as frequently as possible within the acceptable pause time range of the application Cleanup, and perform the cleanup of non-young regions as much as possible.

Outlook

    In the future, the tuning of the JVM may require more tuning for the G1 algorithm.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324860360&siteId=291194637