Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java


In this core java tutorial we will learn about Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java.


Contents of page >
  • Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java
  • 1. Features of Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java  >
  • 2. When to Use the Concurrent Low Pause Collector in java
  • 3. Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC
      • 3.1 Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector with n number of threads in java >
-XX:ParallelCMSThreads=<n>
  • 4. Heap Structure for CMS garbage Collector
    • Diagram of heap Structure for CMS garbage Collector.
  • 5. Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector working in detail in java >
      • 5.1. Major gc(garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
      • 5.2. Minor gc (garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector >
      • 5.3. Young Generation Guarantee in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
      • 5.4. Full Collections in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
  • 6. Fallback with CMS garbage collector in java >
  • 7. Pauses (STW/ stop the world events) in Concurrent Mark Sweep (CMS) garbage Collector / concurrent low pause collector in java >
  • 8. Scheduling pauses in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
  • 9. Detailed Steps in GC (garbage collection) cycle in Concurrent Mark Sweep (CMS) Collector / concurrent low pause garbage collector in java >

    • Young Generation GC (garbage Collection) in java
    • After Young generation GC (garbage Collection) in java
    • Old Generation GC (garbage Collection) with CMS in java
  1. Initial mark phase
  2. Concurrent marking phase
  3. Remark
  4. Sweep phase
  5. Reset phase

  • 10. More Vm (JVM) option for Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar in java>

  • Summary -



Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java

1. Features of Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java  >
  • Concurrent Mark Sweep Collector is also called
    • concurrent low pause collector
    • concurrent low pause GC (garbage collector)

    • CMS GC (garbage Collector)
    • CMS Collector

    • concurrent low pause collector
    • concurrent low pause GC (garbage collector)

  • Concurrent Mark Sweep (CMS) collector collects the old/tenured generation in java.
  • Concurrent Mark Sweep (CMS) Collector minimize the pauses by doing most of the garbage collection work concurrently with the application threads in java.
  • Concurrent Mark Sweep (CMS) Collector on live objects >
Concurrent Mark Sweep (CMS) Collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap in java.

We will discuss detailed working of CMS collector later in this tutorial in java.

Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.

2. When to Use the Concurrent Low Pause Collector in java

  • Concurrent Low Pause Collector should be used if your applications that require low garbage collection pause times in java.
  • Concurrent Low Pause Collector should be used when your application can afford to share processor resources with the garbage collector while the application is running in java.
  • Concurrent Low Pause Collector is beneficial to applications which have a relatively large set of long-lived data (a large tenured generation) and run on machines with two or more processors in java.

Examples when to use  Concurrent Mark Sweep (CMS) collector / concurrent low pause collector should be used for >
Example 1 - Desktop UI application that respond to events,
Example 2 - Web server responding to a request and
Example 3 - Database responding to queries.

3. Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC
Learn how to pass vmargs (VM parameters) to java program in eclipse?

Example of using Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -jar d:\MyJar.jar

3.1 Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector with n number of threads in java >
-XX:ParallelCMSThreads=<n>

Example of using Concurrent Mark Sweep (CMS) collector with n number of threads in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=5 -jar d:\MyJar.jar
Here, 5 will be the number of threads which will be used for garbage collection by Concurrent Mark Sweep (CMS) Collector.

4. Heap Structure for CMS garbage Collector
CMS garbage collectors didies heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.

Young Generation is further divided into Eden, S0 (Survivor space 0) and S1 (Survivor space 1).
Diagram of heap Structure for CMS garbage Collector.

5. Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector working in detail in java >

As mentioned above Concurrent Mark Sweep (CMS) collector collects the old/tenured generation (i.e. performs Major garbage collection process).

5.1. Major gc(garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
For each major collection the CMS collector will pause all the application threads for a brief period at the beginning of the collection and toward the middle of the collection.

The second pause tends to be the longer than first pause and uses multiple threads to do the collection work during that pause in java. The remainder of the collection is done with a garbage collector thread that runs concurrently with the application.

5.2. Minor gc (garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector >
The minor collections is done in a manner similar to the serial collector although multiple threads are used to do the collection in java.

5.3. Young Generation Guarantee in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
The concurrent collector can recover if it starts a young generation collection and there is not enough space in the tenured generation to hold all the objects that require promotion from the young generation.

5.4. Full Collections in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
The CMS collector uses a single garbage collector thread that runs simultaneously with the application threads for garbage collection in tenured generation before it becomes full.

Normally, the CMS collector is able to do most of its work with the application threads still running, so application threads are paused briefly only.

6. Fallback with CMS garbage collector in java >
As a fallback, if the CMS collector is unable to finish before the tenured generation fills up, the application is paused and the gc(garbage collection) is completed with all the application threads stopped.
Such garbage collections with the application stopped are referred to as full collections and are a sign that some adjustments need to be made to the concurrent collection parameters.

7. Pauses (STW/ stop the world events) in Concurrent Mark Sweep (CMS) garbage Collector / concurrent low pause collector in java >
concurrent low pause collector pauses an application twice during a gc(garbage collection) process in java.
  • First pause (also called Initial mark) - mark live/reachable objects (Example - objects on thread stack, static objects etc.) and elsewhere in the heap (Example - the young generation).

No pause phase - Concurrent marking phase -  finds live objects while the application continues to execute.

  • Second pause (also called Remark) - Happens after first pause and concurrent marking phase. It finds objects that were missed during the concurrent marking phase due to the concurrent execution of the application threads.

8. Scheduling pauses in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
The pauses for the young generation collection and the tenured generation collection occur independently. They cannot overlap, but they can occur in quick succession such that the pause from one collection immediately followed by one from the other collection can appear to be a single, longer pause.

9. Detailed Steps in GC (garbage collection) cycle in Concurrent Mark Sweep (CMS) Collector / concurrent low pause garbage collector in java >

Young Generation GC (garbage Collection) in java
  • Live objects are copied from the Eden space and survivor space to the other survivor space.
  • Any older objects that have reached their aging threshold are promoted to old generation.

After Young generation GC (garbage Collection) in java
  • After a young GC, the Eden space and one of the survivor spaces is cleared.
  • promoted objects (older objects that have reached their aging threshold in young GC) are are available in old generation.

Old Generation GC (garbage Collection) with CMS in java
  1. Initial mark phase - (First pause happens/ stop the world event ) - mark live/reachable objects (Example - objects on thread stack, static objects etc.) and elsewhere in the heap (Example - the young generation).

  1. Concurrent marking phase - (No pause phase ) -  finds live objects while the application continues to execute.

  1. Remark - (Second pause happens/ stop the world events) - It finds objects that were missed during the concurrent marking phase due to the concurrent execution of the application threads.

Old Generation GC (garbage Collection) - Sweep phase (Concurrent Sweep phase) in java

  1. Sweep phase -  do the concurrent sweep, memory is freed up.
  • Objects that were not marked in the previous phase are deallocated in place.
  • There is no compaction

Unmarked objects are equal to Dead Objects.

Old Generation GC (garbage Collection) - After Sweeping

  1. Reset phase - do the concurrent reset.

10) More Vm (JVM) option for Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar in java>

  • -XX:+CMSIncrementalMode
This VM option enables the incremental mode.
Concurrent collector must be enabled (with -XX:+UseConcMarkSweepGC) for this option to work.
By default it is disabled.

  • -XX:+CMSIncrementalPacing default: disabled
This VM option enables automatic adjustment of the incremental mode duty cycle based on statistics collected while the JVM is running.

  • -XX:CMSIncrementalDutyCycle=<N>
This is the percentage (0-100) of time between minor collections that the concurrent collector is allowed to run. If CMSIncrementalPacing is enabled, then this is just the initial value.
Default value is 50

  • -XX:CMSIncrementalDutyCycleMin=<N>
This is the percentage (0-100) which is the lower bound on the duty cycle when CMSIncrementalPacing is enabled.
Default value is 10

  • -XX:CMSIncrementalSafetyFactor=<N>
This is the percentage (0-100) used to add conservatism when computing the duty cycle.
Default value is 10

  • -XX:CMSIncrementalOffset=<N>
This is the percentage (0-100) by which the incremental mode duty cycle is shifted to the right within the period between minor collections.
Default value is 0

  • -XX:CMSExpAvgFactor=<N>
This is the percentage (0-100) used to weight the current sample when computing exponential averages for the concurrent collection statistics.
Default value is 25


Summary -

I am highlighting all the summary portion to keep it separate from whole tutorial. Please refer above for complete detail.

1) Features of Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java  >
  • Concurrent Mark Sweep (CMS) collector collects the old/tenured generation in java.
  • Concurrent Mark Sweep (CMS) Collector minimize the pauses by doing most of the garbage collection work concurrently with the application threads in java.

2) When to Use the Concurrent Low Pause Collector in java

  • Concurrent Low Pause Collector should be used if your applications that require low garbage collection pause times in java.
  • Concurrent Low Pause Collector should be used when your application can afford to share processor resources with the garbage collector while the application is running in java.

3) Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC

4) Heap Structure for CMS garbage Collector
CMS garbage collectors didies heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.
Young Generation is further divided into Eden, S0 (Survivor space 0) and S1 (Survivor space 1).

5) Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector working in detail in java >


Major gc(garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
For each major collection the CMS collector will pause all the application threads for a brief period at the beginning of the collection and toward the middle of the collection.

Minor gc (garbage collection) in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector >
The minor collections is done in a manner similar to the serial collector although multiple threads are used to do the collection in java.

Full Collections in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
The CMS collector uses a single garbage collector thread that runs simultaneously with the application threads for garbage collection in tenured generation before it becomes full.

6) Fallback with CMS garbage collector in java >
As a fallback, if the CMS collector is unable to finish before the tenured generation fills up, the application is paused and the gc(garbage collection) is completed with all the application threads stopped.

7) Pauses (STW/ stop the world events) in Concurrent Mark Sweep (CMS) garbage Collector / concurrent low pause collector in java >
concurrent low pause collector pauses an application twice during a gc(garbage collection) process in java.

8) Scheduling pauses in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >
The pauses for the young generation collection and the tenured generation collection occur independently.

9) Steps (In short) in GC (garbage collection) cycle in Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java >

Young GC (Generation garbage) Collection happens. Then,

Than Old Generation GC (garbage Collection) happens.

  1. initial mark > stop all application threads; mark all live objects; resume all application threads
  2. concurrent mark  > do the concurrent mark (one processor is used for concurrent work)
  3. Remark > stop all application threads; do the remark; resume all application threads
  4. sweep > do the concurrent sweep, memory is freed up (one processor is used for concurrent work)
  5. reset  > do the concurrent reset (one processor is used for concurrent work)


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>

 

Different type of garbage collectors in java>

>Serial collector / Serial GC (Garbage collector) in java

>Throughput GC (Garbage collector) or Parallel collector in java

>G1 garbage collector / Garbage first collector in java

>PS Scavenge and PS MarkSweep



Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java

Default garbage collector in java >

>What is default garbage collector for Java 7, 8 and 9


Apache tomcat server,outOfMemory and Garbage collection in java >

>How to set or change permgen size in tomcat server, eclipse?

>How to set, change, increase or decrease heap size in tomcat server and eclipse to avoid OutOfMemoryError ?

>How to pass VM argument to tomcat in eclipse



Few more garbage collection and JVM related tutorials>

What is Java Native Interface(JNI)

Find which garbage collector you are using through cmd and java program

Is it good practice to call System.gc() in Java?

Why warning - ignoring option PermSize?



eEdit
Must read for you :