Java garbage collector's CMS collector

Source: Internet
Author: User


1 , features

CMS collectors are JAVA one of the garbage collectors in a virtual machine. It runs in the old age of the JAVA virtual machine. CMS is the acronym (Concurrent marksweep). the CMS Collector is a collector with the goal of obtaining the shortest recovery pause time. More suitable for the Internet and other occasions, may be the most important collector mode in the Internet;

2 , Advantages

because the collector thread can work with the user thread during the longest concurrent markup and concurrent cleanup process throughout the process , the overall CMS The Collector's memory recycling process is performed concurrently with the user thread. Therefore , the CMS is an excellent collector, with the advantages of concurrent collection, low pauses,some ofSun 's official documents are also called concurrent low-pause collectors ( Concurrent Low Pause Collector).

3 , shortcomings

CMS Collector pair CPU resources are very sensitive

Concurrent design-oriented programs areCPUresources are more sensitive. In the concurrency phase, it does not cause the user thread to pause, but it takes a part of the thread (orCPUresources) and cause the application to slow down, the total throughput is reduced. CMSthe number of recycle threads started by default is (CPUQuantity+3)/4, i.e. whenCPUin the4garbage collection threads are up to a maximum of25%of theCPUResources. But whenCPUInsufficient4a time (e.g.2one), thenCMSThe impact on the user program can become very large, ifCPUwhen the load is relatively large, it also divides half of the computing power to execute the collector thread, which can cause the user program to perform at a sudden decrease in speed50%, which is also very unbearable. To address this scenario, the virtual machine provides a type of "incremental concurrency collector" (Incremental Concurrent Mark sweep/i-cms) ofCMScollector variants, the things done and the singleCPUyearsPCThe idea of a machine operating system using preemptive to simulate a multitasking mechanism is to let the concurrency tag and concurrency cleanupGCthreads, user threads run alternately, minimizingGCThe time of the exclusive resources of the thread, so that the whole garbage collection process will be longer, but the impact on the user program will appear less, the speed is not so obvious, but in the current version,I-cmshas been declared as "deprecated", that is, no longer advocating user use.

CMS Collector cannot handle floating garbage

becauseCMSConcurrent cleanup phase The user thread is still running, with the program running naturally there will be new garbage constantly generated, this part of the garbage appears after the marking process,CMSunable to dispose of them in this collection.GCand then clean it off. This part of the rubbish is called "floating garbage" (Floating Garbage) ". It is also because the user thread needs to be running during the garbage collection phase, that is, to reserve enough memory space for the user thread to use, soCMScollectors cannot wait until the old age is almost completely filled up like other collectors to collect, and a portion of the space needs to be reserved for the program to be used for concurrent collection. Under Default settings,CMScollectors used in the old years68%space will be activated, this is a conservative setting, if the old age in the application is not too fast, you can properly adjust the parameters-xx:cmsinitiatingoccupancyfractionvalues to increase the trigger percentage to reduce the number of memory recoveries for better performance. IfCMSThe memory that is reserved during the run does not meet the needs of the program, and it appears once "Concurrent Mode Failure"Failure, this time the virtual machine will start a fallback plan: temporarily enableSerial Oldcollectors to re-collect garbage from the old age, so that the pause time is long. So say the parameters-xx:cmsinitiatingoccupancyfractionsetting it too high will easily lead to a lot of "Concurrent Mode Failure"failure, performance decreases.

A large amount of space debris is generated at the end of the collection

CMSis a "tag-based-clears the algorithm-implemented collector, which results in a large amount of space fragmentation at the end of the collection. When there is too much space debris, large objects will be assigned a lot of trouble, often in the old age there is still a lot of space remaining, but can not find enough space to allocate the current object, you have to trigger an advanceFull GC. To solve this problem,CMSThe collector provides a-xx:+usecmscompactatfullcollectionswitch parameters for "Enjoy" the end of theFull GCThe service is free of charge with a defragmentation process, and the process of memory grooming is not concurrent. The space debris problem is gone, but the pause time must not be longer. The virtual machine designers also provide another parameter-xx:cmsfullgcsbeforecompaction, this parameter is used to set the number of times that do not compress theFull GCfollowed by a compressed one.

4 , Code

Package COM.GC;

Import java.util.ArrayList;

Import java.util.List;

/**

 * simple. JAVA Virtual machine Memory Reclamation , CMS Use of Collectors

 * Parameters: -xms30m-xmx60m-xmn10m-xx:+useconcmarksweepgc-xx:+printgcdetails

* @author Fan Fangming

*/

public class Easycms {

Public byte[] PlaceHolder =new byte[64 * 1024x768];// placeholder

public static Voidmain (string[] args) throws exception{

Outofmemorybyexpansionsize ();

}

private static Voidoutofmemorybyexpansionsize () throws exception{

List<easycms>list = new arraylist<easycms> ();

while (true) {

Easycms serial =new easycms ();

List.add (serial);

Thread.Sleep (Ten);// Pause Ten milliseconds

}

}

}

5 , Parameters

Parameters: -xms30m-xmx60m-xmn10m-xx:+useconcmarksweepgc-xx:+printgcdetails

-xms30m JVM Minimum 30M

-xmx100m JVM Maximum 100M

-xmn10m Cenozoic Fixed 10M

-xx:+ USECONCMARKSWEEPGC corresponding parnew + CMS Collecting Device

-xx:+printgcdetails Print Details GC

6 , running results

...

[GC [Parnew:9110k->972k (9216K), 0.0056278 secs] 48708k->48702k (60416K), 0.0056664 secs] [times:user=0.00sys=0.00, real=0.01 secs]

[GC [1 cms-initial-mark:47730k (51200K)] 48766K (60416K), 0.0000829 secs] [times:user=0.00 sys=0.00,real=0.00 secs]

[Cms-concurrent-mark:0.003/0.003secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[cms-concurrent-preclean:0.000/0.000 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Gc[yg occupancy:1036 K (9216K)][rescan (parallel), 0.0001092 secs][weak refs processing, 0.0000140 secs][1 Cms-remark: 47730K (51200K)] 48766K (60416K), 0.0001609 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Cms-concurrent-sweep:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Cms-concurrent-reset:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[GC [parnew:9112k->9112k (9216K), 0.0000185 secs][cms:47728k->51175k (51200K), 0.0184446 secs]56840k->56814k (60416K), [CMS perm:2086k->2085k (12288K)], 0.0185255secs] [times:user=0.02 sys=0.00, real=0.02 secs]

[GC [1 cms-initial-mark:51175k (51200K)] 56878K (60416K), 0.0000866 secs] [times:user=0.00 sys=0.00,real=0.00 secs]

[Cms-concurrent-mark:0.003/0.003secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[cms-concurrent-preclean:0.002/0.002 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[cms-concurrent-abortable-preclean:0.000/0.000 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Gc[yg occupancy:5703 K (9216K)][rescan (parallel), 0.0001108 secs][weak refs processing, 0.0000041 secs][1 Cms-remark: 51175K (51200K)] 56878K (60416K), 0.0001523 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Cms-concurrent-sweep:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[Cms-concurrent-reset:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

[GC [Parnew:9103k->9103k (9216K), 0.0000140 secs][cms:51172k->51172k (51200K), 0.0135952 secs]60275k->60269k (60416K), [CMS perm:2085k->2084k (12288K)], 0.0136662secs] [times:user=0.00 sys=0.00, real=0.01 secs]

[Full GC [cms:51172k->51172k (51200K), 0.0041339 secs] 60269k->60269k (60416K), [CMS perm:2084k->2084k (12288K) ], 0.0041729 secs] [times:user=0.01 sys=0.00,real=0.01 secs]

[Gc[1 cms-initial-mark:51172k (51200K)] 60269K (60416K), 0.0000883 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

...

7 , CMS procedure for collector operation

CMS The collector is based on the tag - Clear "algorithm implementation, its operation process is more complicated than the previous collectors, the whole process is divided into 6 steps, including:

Initial tag ( CMS initial Mark )

Concurrency Token ( CMS Concurrent Mark )

Concurrent Pre-cleanup ( Cms-concurrent-preclean )

re-tagging ( CMS Remark )

Concurrent Cleanup ( CMS Concurrent Sweep )

Concurrent Resets ( Cms-concurrent-reset )

where the initial tag, re-tagging these two steps still need to " Stop the World ". The initial tag simply marks the object that the GC Roots can directly relate to, fast, and the concurrent tagging phase is the process of GC Roots tracing . The re-tagging phase is to fix the tag record of the part of the object that caused the markup to change as the user program continues to work during the concurrency tag, which is typically slightly longer than the initial mark-up period, but is much shorter than the time of the concurrent tag. All other actions are concurrent.

corresponding to the specific operating results:

[GC [Parnew:9110k->972k (9216K), 0.0056278 secs] 48708k->48702k (60416K), 0.0056664 secs] [times:user=0.00sys=0.00, real=0.01 secs]

"1. Initial tag"

[GC [1 cms-initial-mark:47730k (51200K)] 48766K (60416K), 0.0000829 secs] [times:user=0.00 sys=0.00,real=0.00 secs]

"2. Concurrency token"

[Cms-concurrent-mark:0.003/0.003secs] [times:user=0.00 sys=0.00, real=0.00 secs]

"3. Concurrent pre-cleanup"

[cms-concurrent-preclean:0.000/0.000 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

"4. Re-Mark"

[Gc[yg occupancy:1036 K (9216K)][rescan (parallel), 0.0001092 secs][weak refs processing, 0.0000140 secs][1 Cms-remark: 47730K (51200K)] 48766K (60416K), 0.0001609 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

"5. Concurrent Cleanup"

[Cms-concurrent-sweep:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

"6. Concurrent Reset"

[Cms-concurrent-reset:0.000/0.000secs] [times:user=0.00 sys=0.00, real=0.00 secs]

Java garbage collector's CMS 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.