"In-depth understanding of the JVM": Hotspot garbage collector

Source: Internet
Author: User

Related Concepts Concurrency and parallelism

These two nouns are concepts in concurrent programming, which can be explained in the context of the garbage collector.

    • Parallel (Parallel): Refers to multiple garbage collection threads working in parallel, but at this point the user thread is still in a wait state.

    • Concurrency (Concurrent): User threads are executed concurrently with the garbage collection thread (but not necessarily in parallel, may be alternately executed), the user program continues to run, and the garbage collector runs on the other CPU.

Minor GC and full GC
    • New Generation GC (Minor GC): Refers to the garbage collection action occurring in the Cenozoic, because most Java objects have the characteristics of being born and going out, so the Minor GC is very frequent, and the general recovery speed is relatively fast.

    • Old age GC (Major gc/full GC): Refers to the GC, which occurred in the old age, Major GC, often accompanied at least once minor GC (but not absolute, in the parallel scavenge collector's collection strategy is directly Major GC's policy selection process). Major GC is typically 10 times times slower than the minor GC.

Throughput

Throughput is the ratio of the CPU's time spent running user code to the total CPU time consumed, i.e.

throughput = Run user code time/(run user code time + garbage collection time).

Virtual machines run for a total of 100 minutes, where garbage collection takes 1 minutes and the throughput is 99%.

Hotspot garbage collector

There is no specific description of how the garbage collector is implemented in the virtual machine specification, so each vendor's garbage collector may be completely different, but we are looking at the JDK1.7-based hotspot virtual machine (including the introduction of the front-facing Java Virtual machine, 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 a garbage collector that can be connected with a double arrow indicates that both 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

The serial collector 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, Serial old collectors work 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. So far, theserial collector is still the default new generation garbage collector in client mode.

parnew Collector

The Parnew Collector is a multithreaded version of the serial collector, and the Parnew collector works as follows:

The Parnew collector is the preferred Cenozoic collector for many virtual machines running in server mode. Removing the performance factor is important because, in addition to the serial collector, only it can work with the CMS collector at this time.

However, in a single-CPU environment, the Parnew collector has absolutely no better effect than the serial collector, and even due to the overhead of thread interaction, the collector is not guaranteed to exceed the serial collector in an environment of two CPUs implemented with Hyper-Threading technology. However, as the number of CPUs that can be used increases, it is beneficial for the efficient use of system resources in GC.

Parallel Scavenge collector

The Parallel scavenge collector is a new generation garbage collector that uses a replication algorithm and is also a parallel multi-threaded collector. There are many similarities compared to the parnew collector, but the parallel scavenge collector is more concerned with the controllable throughput . 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 task of the program as soon as possible.

The Parallel scavenge collector uses two parameters to control throughput:

    • Xx:maxgcpausemillis control 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.

The main significance of the Serial old collector is also in the use of virtual machines in client mode. In the case of server mode, it has two main uses: one for use with the parallel scavenge collector in JDK 1.5 and earlier, and the other for a backup of the CMS collector, which takes place in a concurrent collection concurrent used when Mode failure.

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.

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:

By the way, it is known that the longest concurrent token and concurrent purge process collector threads throughout the process can work with the user thread, so the overall memory recycling process on the CMS collector is performed concurrently with the appetite user thread.

The benefits of CMS are obvious: concurrent collection, low pauses. Since the time spent on garbage collection is mainly consumed by both concurrent tagging and concurrent purging, although the initial markup and re-tagging still require the user thread to be paused, this portion of the time taken is considered to be a low pause, compared to the other two steps in general.

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), and the new generation and the old are part of the region's collection, and the G1 collection is the region (piecemeal).

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 process is as follows:

Summary of common parameters of garbage collector

Client/serrver-Side different GC methods:

Sun JDK hotspot Virtual Machine GC combination method:

Reference
1, Zhou Zhiming, in-depth understanding of Java Virtual machines: JVM advanced features and best practices, mechanical industry Press

"In-depth understanding of the JVM": Hotspot 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.