java - GC garbage collector Detailed (b)

CMS collector

CMS collector (ConcurrentMarkSweep: concurrent mark cleared) is a collection pause for the shortest possible time is the goal of the collector.

Suitable for use on the internet site server or B / S system, with particular emphasis on the response speed of such applications servers, want the system to the shortest pause time.

CMS is ideal for large heap memory, CPU core count and more server-side applications, but also the first choice for collectors G1 appears before large-scale applications.

Concurrent Mark Sweep concurrent mark clear, concurrent low pause collection, concurrency refers to the execution with the user thread

The open collector JVM parameters: -XX: + UseConcMarkSreepGC, this parameter is automatically turned on after the -XX: + UseParNewGC open.

After opening this parameter, use ParNew (Young-zone) + CMS (Old-zone) + SerialOld combination of collector, SerialOld error as a backup CMS collector.

When the CMS has been unable to collect garbage (excessive memory fragmentation) will be using SerialOld FullGc.

CMS conducted four steps of the GC:

  1. The initial mark (CMS initial mark): just mark what objects can be directly associated with the GC Roots , very fast, still need to suspend all work threads
  2. Concurrent with the mark (CMS concurrent mark) and user threads: process GC Roots tracking, and user threads work together, without pausing worker threads . The main course mark, mark all objects
  3. Relabeled (CMS remark): To amend the concurrent marking period, due to the user program continues to run and result in marked record that part of the object from label changes, still need to suspend all work threads . Since concurrent mark, the user thread is still running, so before the official clean-up, then a correction.
  4. Concurrent Clear (CMS concurrent sweep) and user threads together: Clear GC Roots unreachable objects , and user threads work together, without pausing worker threads . Based on the results of labeling, direct clean up the object.

As the longest concurrent mark and concurrent process of clearing, garbage collection thread and a user can work together concurrently, so the overall recovery of memory and user threads CMS collector is together concurrently.

CMS by GC of four steps pictures

CMS advantages: low pause concurrent collection

CMS Disadvantages:

  1. Concurrent execution, pressure on the cpu resources: Due concurrently, CMS in the collection and application threads at the same time will increase the take up of heap memory, that is to say, CMS must be completed before the old garbage collector's heap memory is exhausted, or CMS when recovery fails, it will trigger a security mechanism, the old serial collector's will to
    STW way last GC, resulting in a greater dwell time.

  2. Clear labeling algorithms can not organize space debris, old s space will long as the application is gradually depleted, and finally had to heap memory is compressed by a security mechanism. CMS also provides the parameters -XXlCMSFullGCsBeForeCompaction (default 0, that is performed every time memory consolidation) to specify how many times after CMS collection, once the compression of Full GC.

How to choose the garbage collector

  • Small single CPU or memory, stand-alone program
    -XX: + UseSerialGC
  • Multi the CPU, requires a maximum throughput is calculated as background applications
    -XX: + UseParallelGc or
    -XX: + UseParallelOlGC
  • 多CPU,追求低停顿时间,需快速响应如互联网应用
    -XX:+UseConcMarkSweepGC
    -XX:+ParNewGC
参数 新生代垃圾收集器 新生代算法 老年代垃圾收集器 老年代算法
-XX:+UseSerialGc SerialGc 复制 SerialOldGc 标整
-XX:+UseParNewGc ParNew 复制 SerialOldGc 标整
-XX:+UseParallelGc/-XX:+UseParallelOldGc Parallel[Scavenge] 复制 ParallelOld 标整
-XX:+UseConcMarkSweepGc ParNew 复制 CMS+SerialOld的收集器组合(SerialOld作为CMS出错的后备收集器) 标清
-XX:+UseG1GC G1整体采用标记-整理算法 局部是通过复制算法,不会产生内存碎片

Guess you like

Origin www.cnblogs.com/cjunn/p/12233441.html