Garbage collector in Java

Source: Internet
Author: User

The garbage collector is a specific implementation of memory recycling, the Java virtual machine specification does not specify its implementation, different vendors, different versions of the virtual machine provides a large variety of garbage collectors. This article mainly introduces the garbage collector in the HotSpot virtual machine.

Various garbage collectors

Shows different garbage collectors, each of which is used for specific generations. If the two collectors directly exist, then the two collectors can be used together, such as the new generation using Parnew, the old era using CMS. Each garbage collector has its own advantages and disadvantages, there is no perfect collector, we need to target their own scene, choose the most appropriate collector.

Serial Collector
Serial is the oldest and most basic collector, it has the following two major features:

Serial is a single-threaded collector that uses only one thread to complete the garbage collection work;
Serial the garbage collection work, all other worker threads are suspended until the collection is finished. The so-called Stop the world.
The advantage of Serial is that it is simple and efficient because it is single threaded, no other threads are crossed, so you can concentrate on the garbage collection work and get the highest collection efficiency. At the same time, the disadvantage is obvious, that is, in the garbage collection process will be suspended all other worker threads, if this pause time is too long, so for many applications this is completely intolerable.

Serial/serial old

is an example diagram of the Serial collector running. In the collection of new generation of garbage, the use of replication algorithm, in the collection of the old age, the use of a marker-collation algorithm.

Now the Serial collector is still the default new generation collector in Client mode. For scenarios where there is little space in the Cenozoic, garbage collection pauses can be controlled within dozens of to 100 milliseconds, and this pause is acceptable.

How to use:-XX:+USESERIALGC.

Serial Old Collector
Serial old is an older version of the Serial collector, using a single-threaded collection of garbage from the old age, using the marker-collation algorithm, the previous diagram has been mentioned.

The Serial old collector can be used as a fallback plan for the CMS collector when the CMS collection fails. It is used in the same manner as above:-XX:+USESERIALGC. In fact, this option uses the Serial collector to collect the new generation, using the Serial old collector to collect older generations.

Parnew Collector
The Parnew Collector is a multithreaded version of the Serial collector. It runs in the new generation, using the replication algorithm, and concurrently doing garbage collection work. But concurrency here means using multiple threads for garbage collection instead of executing concurrently with other worker threads, so it still needs to stop the world and pause all other threads. In fact, in addition to the collection time using multithreading, the other behavior is basically consistent with the Serial collector. Here's how it works.

Parnew

The Parnew collector is less effective than Serial in a single-threaded environment. However, with the increase in the number of CPUs that can be used, it is also beneficial to use the Parnew collector. Another important aspect of this is that it works with the CMS to work with each other.

You can use-XX:+USEPARNEWGC to specify the use of the Parnew collector, at which time the old Serial single-threaded collector is used. You can also use the CMS collector by setting-XX:+USECONCMARKSWEEPGC, which uses parnew as the new generation collector by default.

Parallel Scavenge Collector
The Parallel scavenge collector, like the Parnew collector, is also a multi-threaded collector that acts on the new generation using the replication algorithm. However, the Parallel scavenge collector's focus is not how to minimize the downtime of the user thread when garbage collection occurs, but to achieve a controllable throughput. The throughput here refers to the CPU running user code and CPU total time-consuming ratio, for example, the virtual machine ran for 100 minutes, where the user code was running for 99 minutes, garbage collection ran 1 dividends, then the throughput is 99%.

Garbage collection stays short, suitable for programs that need to interact with the user, and a good response speed can improve the user experience. The high throughput can efficiently utilize CPU resources, complete computing tasks as quickly as possible, and be suitable for background operations without too much interaction.

The Parallel scavenge collector provides two parameters to control throughput,-xx:maxgcpausemillis is used to control the maximum garbage collection time, and-xx:gctimeratio is used to directly set the throughput size. Where the Maxgcpausemillis parameter represents a number of milliseconds greater than 0, the collector will ensure that the maximum garbage collection takes no more than the set value when possible. Gctimeratio is an integer greater than 0 and less than 100, indicating the percentage of garbage collection time allowed. If set to 19, the maximum allowable garbage collection time is 1/(19 + 1) = 5%. The default is 99, which is the maximum allowable garbage collection time ratio is 1/(99 + 1) = 1%.

In addition to the above two parameters, the Parallel scavenge collector also has a-xx:+useadaptivesizepolicy parameter, when this parameter is opened, you do not need to manually specify the size of the Cenozoic, the ratio of Eden to Survivor, Promotion old age Object age Deng details parameters, the virtual opportunity according to the current system operation of the dynamic parameter adjustment, to provide the most appropriate pause time and maximum throughput. The adaptive Adjustment strategy is also an important feature.

As can be seen, the Parallel scavenge collector is closely related to throughput, so it is also known as the throughput priority collector. Use the-XX:+USEPARALLELGC parameter to turn on.

Parallel Old Collector
Parallel old is the older version of Parallel scavenge, using the tag-collation algorithm. The Parallel old and Parallel scavenge collectors run as follows:

Parallel Scavenge/parallel Old

CMS Collector
The full name of the CMS collector is Concurrent Mark Sweep, which can be seen from the top of the name, which is based on the tag-purge algorithm, and is a collector that runs concurrently. Its operation is divided into four steps:

Initial tag;
Concurrency token
Re-tagging
Concurrent cleanup
The initial tag phase marks the object that the GC Roots can directly relate to, and is fast. The concurrency token is the process of further marking the reference, which is performed concurrently with the user thread. The re-tagging phase is a token record of the part of the object that is changing as the user program continues to run while the concurrency token is fixed. Finally, concurrent cleanup is the process of garbage cleanup and is executed concurrently with the user thread.

In the above procedure, the initial tag and the re-tagging phase is required to Stop the world, while concurrent tagging and concurrent purging are performed concurrently with the user thread. However, the time-consuming initial tag and re-tagging is much less time consuming than concurrent tagging and concurrent cleanup, so the CMS collector is, in general, concurrent with the user program. Is its run example diagram:

Cms

The advantage of CMS is that garbage collection can be done concurrently, and the pause time is short. However it also has several disadvantages:

Very sensitive to CPU resources. Concurrent garbage removal, will consume the user program's CPU resources, when the number of CPUs is not high, will affect the user program execution speed.
Garbage removal phase, because it is executed concurrently with the user program, so the user program in the process will generate garbage, this part of the garbage can not be carried out by the collector, can only wait for the next garbage collection, that is, "floating garbage."
The mark-and-sweep algorithm generates a lot of space fragmentation, so that when allocating large object space, it is possible that large enough contiguous space cannot be found to allocate, which can cause the full GC to be triggered.
Using the CMS collector with-XX+USECONCMARKSWEEPGC, you can use the-XX:+USEPARNEWGC parnew collector for the new generation of garbage collectors at the same time.

G1 Collector
The full name of the G1 collector is garbage first, which has the following characteristics:

Concurrent execution to shorten the time of Stop the world;
Collection of generations, it does not need to work with other collectors;
Based on the tag-collation algorithm, the memory space fragmentation is not generated, which facilitates the long-term operation of the program;
A predictable pause-time model can be established.
The G1 collector differs from other collectors in that its heap memory layout differs greatly from other collectors. It divides the entire Java heap into areas of equal size, both Cenozoic and older, and is a collection of some of these areas and does not need to be contiguous. G1 calculates the recovery value of garbage accumulation in each region, that is, the amount of space to reclaim and a calculated value for the time it takes, and maintains a prioritized list in the background that is sorted by recycled value, giving priority to reclaim the most valuable areas per time, based on the allowable collection times. This is also the origin of its name.

The operation of the G1 collector is probably the following steps:

Initial tag
Concurrency token
Final tag
Filter Collection
The first few steps are similar to the CMS collector, where the initial marking phase simply marks the object that GC Roots can directly relate to. This phase requires a stalled thread, but it takes a short time. The concurrency token is the continuation of the accessibility analysis of objects in the heap, identifying the surviving objects, which can be performed concurrently with the user thread, although it takes a long time. The final tagging phase is also for remediation. Finally, the recovery stage is filtered, the recovery value and the recovery cost of each region are sorted, and the recovery plan is prepared according to the desired GC time. The above process is as follows:

G1

Use-XX:+USEG1GC to enable the G1 collector.

Garbage collection in Java 8
In Java 8 The garbage collection has been improved, the biggest thing is to remove the permanent band. In the past developers need to carefully adjust the size of the permanent generation, it is easy to cause OOM. Now that the JVM can manage this area on its own, JDK8 HotSpot removes this part of the area and uses local memory to store the information, called the meta-space metaspace.

In addition, Java 8 has been optimized for G1, mainly in string deduplication, where the optimized collector can point a duplicate string object to the same char[] array, avoiding multiple copies in the heap.

Summarize
This paper introduces the different garbage collectors, single-threaded Serial, Serial old, multi-threaded parnew, Parallel scavenge, Parallel old, and the real sense of concurrent CMS, G1, This paper introduces the usage scene, working mechanism and advantages and disadvantages of each collector in brief.

However, this paper does not deeply analyze the working mechanism and implementation principle of each garbage collector, nor does it compare and analyze the usage and performance of each collector from the aspect of practice. The garbage collector is only part of the JVM system, and in addition to understanding the various garbage collector mechanisms, it is also necessary to understand the various garbage collection algorithms and compare their advantages and disadvantages. In addition, we need to strengthen practice and analyze the logs of different collectors.

Garbage collector in Java

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.