Introduction to Java Garbage Collector (GC) and research on the best combination

Source: Internet
Author: User

After nearly 20 years of evolution, Java has developed a complex, robust, and high-performance garbage collector. Using different GC combinations in different applications can improve program performance considerably. I think that's one of the reasons why Java has been position for so many years.

The following discussion is limited to the hotspot JVM in server mode.

Types of GC

Sun/oracle's hotspot JVM provides us with a variety of GC, a GC that is only dedicated to the memory recycling of the new generation or the old age, so we need to specify different GC for the new generation and the old age when actually used. But G1 is the exception, because G1 can take over the entire heap of memory.

Serial GC

The Serial GC is the most basic and oldest GC, which is released by Sun with the first version of Java. Serial is a single-threaded , new -generation GC, so it works on only one thread to complete the GC, and it must also let the JVM suspend execution of all user threads , known as the Stop the world. This means that the program will have a longer pause. So for server-side applications, the Serial GC is largely useless, and the JVM defaults to not selecting this GC for the new generation.

Parnew GC

The Parnew GC is basically a multithreaded version of the serial GC, where multiple thread threads perform cleanup at the same time in the new generation of GC, other than serial. Because it is multi-threading, it will perform better than serial in a multi-CPU environment. However, if it is a single CPU environment, it will lead to the time overhead of thread context switching, but performance will be less serial. Parnew is the default Cenozoic collector for the JVM in server mode

Parallel Scavenge GC

The collector is also a parallel multi-threaded collector acting on the new generation, and the biggest difference with parnew is that it pays more attention to throughput (throughput = time spent running user code/(running user code time + GC execution time)). To achieve this, the GC provides -XX:MaxGCPauseMillis parameters and is designed -XX:GCTimeRatio to control throughput. The former is a millisecond value greater than 0, and GC will try to ensure that garbage collection takes no more time than this value. The latter is an integer greater than 0 and less than 100, which means that garbage collection takes up a percentage of the total elapsed time. In general, if your program does not specifically need to optimize GC throughput, you will not manually specify that the GC be used.

Serial old GC and Parallel old GC

Adding old after the name means that the GC is for the older generation. The former is similar to the serial mentioned earlier in the old age, that is, single-threaded, it will cause a long pause. The latter is equivalent to a multi-threaded version, while working with all user threads first, and then simply starting multiple threads to execute the GC.

CMS GC

The CMS (Concurrent Mark Sweep) is a collector that aims to minimize the payback time, and can only be used in the old age. The preceding GC, when performing the reclamation algorithm, must suspend all user threads before the user thread can continue execution until the GC completes. The biggest difference between CMS and the CMS is that the recovery process can be partially and concurrently executed concurrently with the user thread. The recovery of CMS is divided into the following stages:

    1. Initial tag
    2. Concurrency token
    3. Re-tagging
    4. Concurrent cleanup

Among them, the most time-consuming steps 2 and 4 are executed concurrently, that is, the user thread does not need a pause, and the recycle can be executed concurrently with the user thread, which can greatly shorten the time of the Stop the world. But this does not mean that the CMS is a very perfect GC, and it has the following major drawbacks:

    • The CMS will snatch CPU resources from the user thread. Because the main process of the CMS is executed concurrently with the user thread, the execution speed of the user thread at this time will be somewhat degraded. This can be more serious if you are in a single CPU situation.
    • There may be an error in the CMS Concurrent Mode Failure . Because the CMS in the execution of concurrent tagging, the user thread is also executing, so at the same time there will be a new garbage in the markup, which is called floating garbage, this is the CMS can not be recycled, only wait for the next GC to say. It is also because of the GC when the user thread is also executed that the CMS must reserve a portion of the memory space for use by the user thread before starting the GC, which is risky. This part of the space is not enough for the user thread, which causes concurrent Mode Failure, the JVM will be forced to enable serial old to trigger a full GC, and the time to execute a full GC is longer.

Ps:
-Minor GC: Refers to the process of garbage collection for the Cenozoic, which is usually very fast.
-Full GC: Refers to the GC process performed for the old age, and may be executed once before the full GC is executed. Minor GC. Full GC is usually much slower than minor GC.

G1 Collector

G1 is one of the newest achievements in the development of garbage collection technology, which differs from the previous GC in the following ways:

    • G1 can manage the entire heap area, including the new generation and the old age.
    • G1 is not physically distinguishable from the Cenozoic and the old ages. G1 will divide the whole heap into regions, and the new generation and the old have now changed only the concept of logic, and they do not need to be physically strictly differentiated.
    • G1 will sort all of the region's recovery efficiencies, prioritizing the region with the highest recovery efficiency.

In addition, G1 and CMS are also performed concurrently by the GC, that is, the cleanup can be performed concurrently with the user thread (concurrent), but the G1 can be a shorter time to pause than the CMS.

GC's selection
    • For service-side applications, I personally believe that G1 should be used preferentially. In addition to the many outstanding features of G1, one important reason is that Oracle intends to replace all of the preceding GC with G1 in the future, which means that Oracle will definitely focus on G1 for GC optimization in the future. But Oracle is still biased, because until today's Java8, if you don't specify a GC, the JVM is still a GC combination that uses Parnew + Serial old. But we can get through
-XX:UseG1GC

The Parameters command JVM uses G1.

    • For client applications, the JVM chooses the Serial + Serial old combination If no parameters are added, and it is not a force. It is best to add:
-XX:+UseConcMarkSweepGC

That is, command the JVM to use CMS in the old age to improve GC performance.

Introduction to Java Garbage Collector (GC) and research on the best combination

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.