Java garbage collection mechanism

Source: Internet
Author: User

Java garbage collection mechanism

The blog named Java garbage collection mechanism, giving people feel like garbage collection is unique to the Java language. In fact, garbage collection (garbage Collection) is far more remote than Java. There are 3 things to consider in garbage collection: which memory needs to be recycled, when it is recycled, and how it is recycled. With these three questions, let's take a look at how Java is garbage collected.

What parts of the Java garbage collection (GC) mechanism work primarily on the runtime data area? In the previous blog, "How the Java Virtual machine Works," we introduced the JVM Runtime data area has program counters, virtual machine stack, local method stack, heap, method area 5 regions. The first three regions are created with the creation of threads and die out with the demise of the thread, and stack frames in the stack execute the stack and stack operations methodically as the method enters and exits. Therefore, the three regions do not need to consider the garbage collection problem too much. While the Java heap and the method area are different, multiple implementations of an interface require memory that may be different, and multiple branches in one method may require different memory, and only when the program is running can you know which objects are created, and that the allocation and collection of memory is dynamic. So the garbage collector is concerned with this part of the memory.

Back to the first thing about garbage collection: which memory needs to be recycled? The Java heap holds almost all of the object instances in the program, and the garbage collector first needs to determine which objects are "alive" and which have "died" before the heap is recycled. The usual method of judging is the reference counting algorithm and the accessibility analysis algorithm. The reference counting algorithm adds a reference counter to the object, the counter value is incremented by 1 whenever a reference is made to it, and the counter value is reduced by 1 when it is invalidated, and if the value of the counter is 0, the object is no longer used (dead). However, there is no counting algorithm in the Java Virtual machine to manage memory, because the reference counting algorithm is difficult to solve the problem of circular referencing between objects. The accessibility analysis algorithm is a series of objects called "GC Roots" as the starting node, starting from these nodes to search down, the path of the search is called the reference chain, when an object to the GC Roots no reference chain connected, it proves that this object is not available (also died). Among the objects that can be used as GC roots objects are the object referenced in the virtual machine stack (the local variable table in the stack frame), the object referenced in the static property, the object referenced in the method area, and the object referenced in the local method stack. This is all about memory recycling in the Java heap, and the garbage collection of the method area (the permanent generation in the hotspot) mainly reclaims two parts: obsolete constants and useless classes. Determining whether a constant is an obsolete constant is simply a matter of determining if there are any objects that have references to the constant. And to judge the useless class needs to satisfy 3 conditions: All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap; the ClassLoader that loaded the class have been recycled The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed anywhere by reflection on the method of the class.

garbage Collection Algorithm
    • Tag-purge algorithm (Mark-sweep algorithm)

The algorithm is divided into two parts (mark, clear), first mark out all the objects that need to be recycled, after the mark is complete, all the tagged objects are collected uniformly. The algorithm mainly has two shortcomings: one is the efficiency problem, the labeling and elimination of two processes is not efficient; one is a spatial problem, and a marked sweep generates a lot of memory fragmentation. The execution of the markup cleanup algorithm is as follows:

    • Replication Algorithms

In order to solve the tagging-clearing algorithm efficiency problem, the replication algorithm divides the available memory equally, using only one part at a time, and when the used part is exhausted, copies the surviving objects into a block, then clears the used parts once. This avoids the amount of memory fragmentation, but the utilization of the memory space is not high. The replication algorithm executes as follows:

  Today's commercial virtual machines are using this approach to the new generation of mobile phones, because the new generation of object 98% is "dying", so do not need to allocate memory space according to 1:1, but the memory is divided into a larger Eden space and two smaller survivor space. When recycled, objects that are still alive in Eden and survivor are copied to another survivor space at once, and the Eden and the used survivor space are cleared. If the object space in Eden and Survivor is larger than the other survivor space when reclaimed, these surviving objects can enter the old age directly through the allocation guarantee mechanism.

    • Tagging-sorting algorithms

The replication algorithm requires more replication operations when the object has a higher survival rate, and the efficiency becomes lower. and the replication algorithm wastes 50% of the memory space. The survival rate of the object is higher in the old age, so it is not possible to choose the copy algorithm directly in the old age. According to the high survival rate of the old age, the "mark-and-sweep" algorithm comes into being, the algorithm is the first to mark all the objects that need to be recycled, and then move the surviving objects to one end and then directly clean out the memory outside the end boundary. As shown in the following:

Generational collection Algorithms

The algorithm does not have new ideas, but divides the memory into several blocks based on the different life cycles of the objects. In general, the Java heap is divided into the new generation and the old age, so that according to the characteristics of the various eras to adopt appropriate collection algorithm, for the new generation of the characteristics of the death of a large number of objects, select the replication algorithm; for the old age of high object survival characteristics, using the "mark-clear" or "mark-collation" algorithm for

garbage collector

Garbage collector is the implementation of the garbage collection algorithm, we first look at the JDK 1.7 Update 14 after the hotspot virtual machine contains all the collectors, we come to the analysis of the following garbage collector.

    • Serial collector, Serial old collector

The serial collector is a single-threaded collector that must stop all other worker threads when a single thread completes the garbage collection work and reclaims the garbage. The new generation of the serial collector takes a replication algorithm for garbage collection. The serial old collector pauses all user threads for the older generation using the tag-collation algorithm. The work process is as follows:

    • Parnew Collector

The Parnew Collector is a multithreaded version of the serial collector, and in addition to using multiple threads for garbage collection, the rest of the behavior, such as the control parameters of the serial collector, the collection algorithm, the pause, and the recovery strategy, are identical to the serial collector. The work process is as follows:

Parnew is the preferred new generation collector for virtual machines in server mode, and in the Cenozoic, only the Parnew collector can work with the CMS collector in addition to the serial collector.

    • Parallel scavenge collector, Parallel old collector

The Parallel scavenge collector is a new generation collector and is also a parallel multi-threaded collector using the replication algorithm. It is different from the focus of the Parnew collector, and its goal is to achieve a controllable throughput, throughput = Run user code time/(run user code time + garbage collection time). The higher the throughput, the higher the CPU utilization, and the task of completing the program as soon as possible, which is mainly suitable for the background operation without the need of too much interaction. The Parallel scavenge collector provides two parameters for precise control of throughput, respectively, to control the maximum garbage collection pause time-xx:maxgcpausemillis parameters and to directly set throughput size-xx:gctimeratio parameters. Since the parallel scavenge collector is closely related to throughput and is often referred to as the "throughput first" collector, the collector has the-xx:+useadaptivesizepolicy parameter except for the last two parameters, which, when opened, You do not need to manually specify the size of the Cenozoic (-XMN), the ratio of Eden to Survivor (-xx:survivorratio), the size of the old object (-xx:pretenuresizethreshold), and other detail parameters, The virtual opportunity collects performance monitoring information based on the operation of the system, dynamically adjusting these parameters to provide the most appropriate pause time or maximum throughput.

The Parallel old collector is an older version of the Parallel scavenge collector, using multithreading and tagging-finishing algorithms. High throughput can be achieved with the combination of parallel old and parallel scavenge collectors.

    • CMS collector

The CMS (Concurrent Mark Sweep) collector is a collector that targets the shortest recovery pause time. From the name, the CMS collector is implemented based on the "tag-purge" algorithm, which is more complex than other collectors, and the process is divided into 4 steps: initial tag, concurrency token, re-tagging, concurrency cleanup. Where initial tags, re-tagging still need to pause all threads. The initial tag simply marks the object that the GC roots can directly relate to. Concurrent tagging is the process of GC Roots tracing. The re-tagging is to fix the tag record of the part of the object that caused the markup to change during the concurrency tag, which is usually slightly longer than the initial marker phase, but is much shorter than the time of the concurrent tag. But the collector's longest concurrency token and concurrent cleanup can be performed with the user thread. The working process is as follows:

CMS is a low-pause collector, but there are a few 3 obvious drawbacks:

The 1.CMS collector is very sensitive to CPU resources, while the concurrency token, concurrency cleanup phase does not cause the user thread to pause, but consumes a portion of the CPU resources and causes the application to slow down and the overall throughput to degrade. The number of recycle threads initiated by the CMS by default is (number of CPUs +3)/4. CMS may have a greater impact on user programs when the number of CPUs is low.

2.CMS collectors cannot handle floating garbage, and "Concurrent Mode Failure" may occur, resulting in a full GC. Because the user thread is still running in the concurrent cleanup phase, there is a new garbage generation, and the CMS cannot be processed in the secondary collection, and it needs to be left to the next GC for cleanup. This part of the rubbish is floating rubbish. It is because of the concurrent cleanup phase that the user thread is still executing, that is, the need to reserve enough memory space for the user thread to use, so the CMS collector cannot wait for the old age to be almost completely full before collecting it, and it needs to reserve a portion of the space for the program to run when it is collected concurrently. The "Concurrent Mode Failure" failure occurs when the reserved memory is not met during the CMS run, triggering the serial old collector for garbage collection.

3.CMS is a collector based on the tag-purge algorithm, resulting in memory fragmentation. To solve the memory fragmentation problem, the CMS provides a-xx:+usecmscompactatfullcollection switch parameter to enable the merge process of memory fragmentation when the CMS collector is FULLGC, and the memory grooming process is not concurrent. So the pause time will grow longer. The virtual Machine Designer also proposes the-xx:cmsfullgcsbeforecompaction parameter, which is used to set the number of uncompressed full GC executions, followed by a compressed (default value of 0, which means defragmentation every time it enters the full GC).

    • G1 Collector

The G1 (Garbage-first) collector is a garbage collector for service-side applications, and G1 has the following features compared to other GC collectors:

1. Parallel and concurrent: G1 take full advantage of the hardware advantages of multi-CPU, multi-core environment, using multiple CPUs to shorten the time of pause, some other collectors originally need to pause Java thread to perform GC action, G1 collector can still be in a concurrent way for Java program to continue execution.

2. Generational collection: Although G1 can independently manage the entire GC heap without the need for other collector mates, it retains the concept of generational. It is able to handle newly created objects in different ways and old objects that have survived for a period of time and have survived multiple GC for better collection results.

3. Spatial integration: Unlike CMS's "tag-clean" algorithm, G1 is a collector based on the "tagging" algorithm, and is implemented locally based on the "copy" algorithm.

4. Predictable pauses: This is another big advantage of G1 relative to the CMS, the reduction of the pause time is a common concern of G1 and CMS, but G1 in addition to the pursuit of low pauses, but also to establish a predictable pause time model, can let the user explicitly specified in a length of M milliseconds in a time fragment, The time spent on garbage collection must not exceed n milliseconds.

While other collectors are collected in the new generation or the old, the Java heap memory layout differs greatly from other collectors when using the G1 collector, which divides the Java pair into separate areas of equal size, while preserving the concept of the Cenozoic and the old, but the new generation and the old age are no longer physically isolated, They are all part of the region's collection.

The G1 collector is able to establish a predictable pause-time model because it can plan to avoid garbage collection throughout the heap. G1 tracks the value of the garbage accumulation in each region (the amount of space needed to reclaim and the experience of the time it takes to reclaim), maintains a prioritized list in the background, and prioritizes recovery of the region with the largest value per time, based on the allowable collection times, This improves the collection efficiency that is obtained as high as possible in a limited time period.

  G1 is the memory "piecemeal", but each region is not irrelevant, so in a region of garbage collection, the object to make the accessibility of the decision to determine whether the object is alive, do you still need to scan the entire Java heap to ensure correctness? In fact, in the G1 collector, the object reference between region, the virtual machine uses remembered set to avoid scanning the whole heap. Each region has a remembered Set, and the Virtual Machine Discovery program generates a write barrier temporarily interrupt write operation when the reference type of data is written. Checks whether the object referenced by reference is in a different region, and if so, logs the relevant reference information to the remembered set of the region to which the referenced object belongs, by Cardtable. When memory reclamation is in progress, adding the remembered set to the enumeration scope of the GC root node guarantees that no full heap scan will be missed.

If you do not calculate maintenance remembered set operations, the G1 collector operates roughly in 4 steps: initial tag, concurrency tag, final tag, filter collection. The initial tag phase simply marks the object that GC roots can directly relate to, and modifies the value of Tams (next Top at Mark Start), allowing the next user program to run concurrently, creating a new object in the correctly available region. This phase requires a stalled thread, but it takes a short time. the concurrency tagging phase is a time-consuming process that can be performed concurrently with the user program, starting with a roots analysis of the objects in the heap from GC to find the surviving objects. the final marking phase is to fix the tag record that is causing the markup to change as the user program continues to work during the concurrency tag, and the virtual machine will record this time object change in the thread remembered Set logs. The final tagging phase requires merging data from the remembered set logs into the remembered set, which requires a stalled thread but can be executed in parallel. Finally, the recovery value and cost of each region are sorted first in the filter recovery stage. A recovery plan is developed based on the expected GC pause time for the user.

Java garbage collection mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.