CMS collector and G1 collector pros and cons __java basics

Source: Internet
Author: User
Tags garbage collection

The CMS collector is a collector that aims to obtain the shortest recovery time, the CMS collector is based on the "tag-clean" algorithm, and the whole process is divided into four steps: 1. Initial tag

2. Concurrent markup

3. Re-marking

4. Concurrent cleanup

Initial tag: Just mark the object that GC roots can directly relate to, fast

Concurrent markup: Is the process of GC roots tracing testing

Re-marking: the re-tagging phase is to modify the tag record of the part of the object that caused the markup to change as the user program continues to run during the concurrent markup, which is typically longer than the initial marking phase and is far shorter than the concurrent markup phase

Advantages: Concurrent collection, low pause

Reason: Because the most time-consuming concurrent markup and concurrent purge process collector programs can work with user threads throughout the process, the CMS Collector's memory recycle process is generally performed concurrently with the user thread

Disadvantages:

1.CMS collectors are very sensitive to CPU resources

In the concurrency phase, the virtual machine provides an "incremental concurrency collector" to address this situation, although it does not cause the user thread to pause, but it slows down the application because it takes up a portion of the thread.

CMS collector variants that allow GC threads and user threads to run alternately while concurrent markup and concurrency cleanup, minimizing the time that GC threads monopolize resources, so that the entire garbage collection process gets longer, but the impact on user programs decreases. (The effect is not obvious, not recommended)

2. CMS processor cannot handle floating garbage

The CMS thread is still running during the concurrency cleanup phase, along with the operation of the program will naturally generate new garbage, this part of the garbage generated in the labeling process, the CMS can not be processed in the process, so only wait until the next GC to clean out, this part of the rubbish is called "floating garbage",

3. The CMS is based on the "tag-purge" algorithm, so there will be a large amount of space debris generated at the end of the collection. Too much space debris, will give large objects of the allocation of a lot of trouble, often there will be a lot of old age and a lot of space remaining, but can not find enough contiguous space to allocate the current object, can only trigger the full GC in advance.

To solve this problem, CMS provides a switch parameter for the CMS to open the full GC when the memory fragmentation of the consolidation process, the process of memory collation can not be concurrent, space debris is gone, but the pause time has become longer


--------------------------------------------------------------------------------------------------------------- ---


G1 is a server-side application-oriented garbage collector. G1 have the following characteristics:

1, parallel to concurrency: G1 can make full use of CPU, multi-core environment hardware advantages, using multiple CPUs (CPU or CPU core) to shorten the Stop-the-world pause time. Some other collectors would have to pause the GC action performed by the Java thread, and the G1 collector can still let the Java program continue to execute in a concurrent manner.

2. Generational collection: Although G1 can manage the entire GC heap independently without the need for other collectors, the concept of generational is retained. It can handle the newly created objects in different ways and the old objects that have been alive for some time and have survived many times to get a better collection effect.

3, Space integration: and CMS "Mark-clean" algorithm, G1 from the overall view is based on the "tag collation" algorithm to implement the Collector, from the local point of view is based on the "Replication" algorithm.

4. Predictable pauses: This is another major advantage of G1 compared to CMS, reducing the pause time is G1 and CMS common concern, but G1 in addition to the pursuit of low pause, but also to establish a predictable pause time model, allowing users to explicitly specify in a length of M-millisecond time fragment,

5, G1 operation steps:

1. Initial Mark 2, concurrent mark; 3, final mark; 4, Filter recycle

There are many similarities between the operation of the above steps and the CMS. The initial markup phase simply marks the object that GC roots can directly relate to, and modifies the value of Tams to create new objects in the correctly available region when the next stage user program is running concurrently, which requires a pause thread, but it takes a short time and the concurrent markup phase is from GC Root begins a accessibility analysis of the objects in the heap, identifying the surviving objects, which take a long time, but can be executed concurrently with the user program. The final marking stage is to fix the part of the tag record that the user program continues to operate during the concurrent tag, and the virtual machine will record this time object in the thread remenbered set logs, the final marking stage needs to put remembered set The logs data is merged into the remembered set logs, and the final markup phase needs to merge the remembered set logs data into the remembered set, which requires a standstill thread but can be executed in parallel. Finally, the recovery value and cost of each region are sorted in the filtering and recycle phase, and the recovery plan is made according to the GC pause time expected by the user.





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.