Learn JVM (5) & mdash; garbage collector, learn jvm garbage collection

Source: Internet
Author: User

Learn JVM easily (5) -- Garbage Collector and jvm garbage collection

Easy to learn about JVM (5)-Garbage Collector

In the previous article, we introduced common garbage collection algorithms. Different algorithms have their own advantages and disadvantages. In JVM, it is not simply a method used for garbage collection, instead, different garbage collection algorithms are packaged in different garbage collectors. Users can use different garbage collectors according to their own needs, so that their java program performance can reach the best.

Before introducing the Garbage Collector, let's review the java Heap Structure.

Heap Memory Review

The memory structure of the java heap includes: the new generation and the old generation. The New Generation consists of an edian zone and two surviving zones. The two surviving zones are identical in size and completely symmetrical, with no difference. We call them S0 and S1, or from and.

JVM garbage collection is mainly for garbage collection of the above heap space. Of course, garbage collection is also conducted for the metadata zone (permanent zone). Here we will mainly introduce garbage collection of heap space.

The following describes several garbage collectors:

Serial collector

As the name implies, the serial collector uses a single thread for garbage collection. The replication algorithm is used for the collection of new generation, and the tag compression algorithm is used for the old generation. This is also consistent with the advantages of the algorithm described in the previous article.

The serial collector is the oldest and most stable collector. Although it is a series collector and has a long recovery time, its stability is better than that of other recyclers. In general, it is a good choice. To use the serial collector, you can add the following parameters when starting the Configuration:

-XX: + UseSerialGC

The execution process of the serial recycler is as follows:

When garbage collection is executed, the application thread is suspended, the GC thread starts (STARTS garbage collection), and the Application Thread continues to execute after the garbage collection is completed. Note: A single thread is used for serial collection during GC thread running.

Parallel recycler

You may have guessed that the parallel recycler uses multiple threads for parallel collection. However, it is important to note whether parallel collection is used for both the new generation and the old generation. There are different recycler options:

1. ParNew recycler

This recycler only recycles concurrent data for the new generation, and serial data is still used in the old generation. The recycling algorithm is still the same as the serial recycling algorithm. The new generation uses the replication algorithm and the old generation uses the mark compression algorithm. In multi-core environments, the performance is obviously better than that of the serial recycler. To use this recycler, you can configure it in the startup parameters:

-XX: + UseParNewGC

To specify the number of concurrent threads, you can configure the following parameters:

-XX: ParallelGCThreads

Shows the process of the ParNew recycler:

The application thread is suspended during garbage collection. The GC thread starts garbage collection in parallel. After the garbage collection is completed, the Application Thread continues to execute.

2. Parallel recycler

It is still a parallel recycler, but this recycler has two types of configuration, one is similar to ParNEW: Parallel recycler for the new generation, and serial recycler for the old generation. It differs from ParNew in that it pays more attention to throughput in the design goal and can be considered to be better than ParNew under the same conditions. To use this recycler, you can configure it in the startup program:

-XX: + UseParallelGC

Another configuration of the Parallel recycler is different from that of ParNew. It is applicable to Parallel collection for both the new and old generations. To use this type of recycler, you can configure it in the startup program:

XX: + UseParallelOldGC

The process of the Parallel recycler is the same as that of ParNew:

During the recycle process, the application is paused, and the GC uses multiple threads for concurrent recycle. After the recycle process is completed, the Application Thread continues to run.

CMS recycler

CMS recycler: Concurrent Mark Sweep, and the Concurrent Mark is cleared. Note: pay attention to the following two words: concurrent and Mark clearing.

Concurrency indicates that it can be executed concurrently or alternately with the application. Tag clearing indicates that the recycler does not use the tag compression algorithm, which is different from the serial recycler and concurrent recycler described earlier. It should be noted that the CMS recycler is a type of recycler for the old generation and does not play a role in the new generation. The advantage of this recycler is that it reduces the pause time of the application because it does not need the application to complete the tentative wait for garbage collection, but is executed concurrently with the garbage collection. To execute this garbage collector, you can configure it in the startup parameters:

-XX: + UseConcMarkSweepGC

The CMS recycler's operating mechanism is very complex. We simply divide its running process into the following steps:

Initial tag

Mark objects that can be directly accessible from the GC Root;

Concurrent mark (together with the application thread)

Mark the process and all objects;

Remark

Because the user thread is still running during the concurrent mark, re-mark and correct the code before it is cleared.

Concurrent cleanup (with the user thread)

Directly clears Objects Based on tag results.

The process is shown in:

The marking process consists of three steps: initial marking, concurrent marking, and re-marking. Concurrent marking is the most important marking process, which is executed concurrently, it can be performed simultaneously with the application thread. Although the initial tag and remark cannot be concurrently executed with the application, the two processes are marked fast and time-consuming, therefore, it will not have a big impact on the application. The process of concurrent cleanup is also performed with the application, avoiding the pause of the application.

The advantage of CMS is that it reduces the pause time of the application and enables concurrent execution of the recycle thread and application thread. But it is not perfect, as can be seen from its operating mechanism, because it does not concentrate on garbage collection for a period of time like other recyclers, and the application is still running during garbage collection, therefore, it is not completely recycled. This also leads to a higher recovery frequency of CMS than other recyclers, and frequent recovery will affect the application throughput.

G1 recycler

The G1 recycler is a recycler launched after jdk1.7, trying to replace the CMS recycler.

Unlike other recyclers, G1 divides the heap space into mutually independent blocks. Each region may belong to both the old age and the new generation, and the space of each region can be discontinuous (compared with the old and new generations of CMS must be continuous ). This idea of dividing old-age districts into multiple blocks stems from the idea that when concurrent background threads look for recyclable objects, some blocks contain much more recyclable objects than other blocks. Although G1 still needs to suspend the application thread when clearing these blocks, it can take a relatively small amount of time to recycle the blocks that contain the most garbage. This is why G1 is named Garbage First: the block with the most Garbage disposal will be processed in the First time. To use the G1 recycler, you must configure the following parameters at startup:

-XX: + UseG1GC

Compared with the CMS recycler, G1 has the following advantages:

1. Because many blocks are divided, memory fragments are reduced during recovery;

2. G1 is applicable to the new generation and old generation, while CMS is only applicable to the old generation.

Summary

This article briefly introduces the Garbage Collector in JVM, including the serial collector, parallel collector, CMS collector, and G1 collector. They both have advantages and disadvantages. Generally, You need to test the performance of the Garbage Collector Based on your business and then make a selection. The following are frequently used parameters for configuring the recycler:

-XX: + UseSerialGC: Serial collectors are used in the new and old generations.

-XX: + UseParNewGC: Parallel collectors are used in the new generation.

-XX: + UseParallelGC: the new generation uses parallel collection collectors to focus more on throughput.

-XX: + UseParallelOldGC: Parallel collector used in earlier years

-XX: ParallelGCThreads: specifies the number of threads used for garbage collection.

-XX: + UseConcMarkSweepGC: the new generation uses parallel collectors, while the old generation uses CMS + Serial collectors.

-XX: ParallelCMSThreads: sets the number of CMS threads.

-XX: + UseG1GC: enables the G1 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.