Deep understanding of JVM Four: a detailed garbage collector

Source: Internet
Author: User

Objective

The garbage collection algorithm has been described in more detail here, we will introduce the specific garbage collector in the JVM, in the virtual machine specification is not how to implement the garbage collector, so each manufacturer's garbage collector may be completely different, However, we are introducing the JDK1.7-based hotspot virtual machine (including the introduction of the front-facing Java Virtual machine is also based on the jdk1.7 version). In the hotspot, the collector of the virtual machine mainly has the following:

You can see that the garbage collector is divided by the generational of the object, and the garbage collectors that can be connected by lines indicate that they can be used together. Can see the new generation of garbage collector has Serial, parnew, Parallel scavenge,g1, belongs to the old age of garbage collector has CMS, Serial, Parallel Old and G1. The G1 is a garbage collector that can be recycled for both new-generation objects and older-age objects. However, in all garbage collectors, there is no commonly used garbage collector. In different scenarios, each garbage collector has its own advantages.

Serial collector

Serial is the most basic and longest-growing collector. It is a single-threaded garbage collector, which means that it needs to suspend other threads when it is garbage collected, the "Stop the World" mentioned earlier. Although this process is the user is not visible in the case of the user normal thread all stop, it sounds a bit harsh, this is very difficult to accept. The serial collector works as follows:

Although not acceptable to the above, but the serial collector has its advantages: simple and efficient, for the limit of a single CPU environment, the serial collector because there is no thread interaction overhead, concentrate on garbage collection naturally can get higher cell phone efficiency . In addition, so far, the serial collector is still the default new generation garbage collector in client mode.

Parnew Collector

This collector can be understood as a multithreaded version of the serial collector, and the Parnew collector works as follows:

Parnew collectors are many of the default generation garbage collectors running in server mode, why not use serial as a new generation collector? Mainly in addition to the serial collector, only the Parnew collector can work with the CMS collector at this time.

Parallel Scavenge Collector

Parallel Scavenge Collector is a new generation garbage collector, which uses the algorithm is the replication algorithm, is also a parallel multi-threaded collector (so-called "parallel", that is, multiple garbage collection threads work simultaneously, but the user is still in a waiting state; there is a similar concept of "concurrency", Refers to a user thread executing concurrently with the garbage collection thread). This parnew is the same as the parallel scavenge collector, so what are the new features of the parallel scavenge collector?

The difference is that the parallel scavenge collector is more concerned with the controllable throughput , the throughput equals the time that the user code was run/(the time the user code was run + garbage collection Time).

What is the point of this parameter? According to the knowledge of data, the higher the throughput, the shorter the garbage collection time, then the user code can make full use of the CPU resources and complete the operation task of the program as soon as possible.

The Parallel scavenge collector uses two parameters to control throughput:-xx:maxgcpausemillis controls the maximum garbage collection pause time,-xx:gcratio directly sets the size of the throughput.

Intuitively, as long as the maximum garbage collection pause time is smaller, the throughput is higher, but the shortening of the GC pause time is at the expense of throughput and Cenozoic space . For example, the original 10 seconds collected once, each pause 100 milliseconds, now become 5 seconds to collect, each pause 70 milliseconds. While the pause time drops, the throughput also decreases.

In addition, the Parallel scavenge collector can also set parameter-xx:+useadaptivesizepocily to dynamically adjust the pause time or maximum throughput, which is called the GC Adaptive throttling Strategy , This is not what the Parnew collector does.

Serial Old Collector

The Serial old collector is an older version of the Serial collector and is a single-threaded collector that is recycled using the "mark-and-Organize algorithm". It runs the same way as the serial collector.

Parallel Old Collector

The Parallel old collector is an older version of the Parallel scavenge collector, which uses multithreading and the "mark-and-organize" algorithm for garbage collection. Typically used in conjunction with the parallel scavenge collector, the "throughput first" collector is a feature of this combination that can be used in situations where throughput and CPU resource sensitivity are a priority. The working process for this combination is as follows:

CMS collector

The goal of the CMS collector (Concurrent Mark Sweep) is to get the shortest recovery pause time. The CMS collector is a better choice when it focuses on the server's responsiveness and wants the shortest pause time.

The entire execution process is divided into the following 4 steps:

    • Initial tag
    • Concurrency token
    • Re-tagging
    • Concurrent cleanup

Both of the initial and re-tagging steps still need to pause the Java execution thread, and the initial tag is just the object that the GC Roots can relate to, and the concurrency token is the process of performing GC Roots tracing. The re-tagging is to fix the record that the tag has changed due to the execution of the user program during the concurrent tag, which makes the tag error. The following procedures are performed:

The benefits of the CMS are obvious: concurrent collection, low pauses (because the time spent on garbage collection is primarily consumed by concurrent tagging and concurrent cleanup, although the initial markup and re-tagging still require a pause on the user thread, this portion of the time taken is less than the other two steps in general, so it can be considered a low pause).

However, the disadvantages of the CMS collector are obvious:

    • Too sensitive to CPU resources, this can be understood, although the user thread is not paused during the concurrent tagging phase, but the program responds slowly because the collector consumes a portion of the CPU resources
    • The CMS collector cannot handle floating garbage. The so-called "floating garbage", is in the concurrent tagging phase, because the user program is running, then naturally there will be new garbage generation, this part of the garbage is marked, the CMS can not be processed in the secondary focus on them (why?). The reason is that the CMS is to get the shortest pause time for the goal, naturally not in a garbage disposal process to spend too much time, and had to be processed in the next GC. This part of the unhandled garbage is called "floating garbage."
    • Because the CMS collector is based on the "tag-purge" algorithm, said earlier that this algorithm will lead to a large number of space debris, once the space fragmentation, large objects can not allocate memory, so even if the memory has left space to accommodate the large object, but there is no continuous large enough space to put down this object, So the virtual machine triggers a full GC (which is also mentioned later) and the problem is solved by controlling the parameter-xx:+usecmscompactatfullcollection, Used to open the merge process of space debris when the CMS garbage collector is not FULLGC.

G1 Collector

The G1 (Garbage-first) collector is one of the newest achievements in today's collector technology and has been in the experimental phase until after Jdk7u4, and is officially used as a commercial collector.

Compared to the previous collectors, the G1 collector has the following features:

    • Parallelism and concurrency
    • Generational collection (still retains the concept of generational)
    • Spatial integration (a "mark-and-organize" algorithm as a whole that does not result in space fragmentation)
    • Predictable pauses (more advanced than CMS in that it allows the user to explicitly specify a time fragment with a length of m milliseconds and consumes no more than n milliseconds for garbage collection)

In addition, the G1 collector divides the Java heap into regions of equal size (independent areas), the new generation and the old are part of the region, and the G1 collection range is

G1 's work process is as follows:

    • Initial tag (Initial marking)
    • Concurrency token (Concurrent marking)
    • Final mark (Final marking)
    • Filter collection (Live Data counting and evacuation)

The initial tagging phase simply marks the object that GC roots can directly relate to, and modifies the value of the Tams (next Top at Mark Start), allowing the next stage of the user program to run concurrently, creating the object in the correct usable region, which requires suspending the thread. The concurrency tagging phase performs the accessibility analysis from GC roots, identifying the surviving objects, which are performed concurrently by the appetite user thread. The final marking phase is the correction of the part of the record that caused the markup to change during the concurrency tagging phase because of concurrent execution of the user program, which is saved in the remembered set logs, and the final tag phase merges the records in the logs into the remembered set. This phase is executed in parallel, and the user thread still needs to be paused. Finally, the recovery value and cost of each region are sorted first in the filtering stage, and the recovery plan is made according to the expected GC pause time. The entire execution has been successful as follows:

Summary of common parameters of garbage collector

Deep understanding of JVM Four: a detailed garbage collector

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.