CMS收集器

CMS收集器是JAVA虛擬機中垃圾收集器的一種。它運行在JAVA虛擬機的老年代中,是基於“標記-清除”算法實現的。

示例代碼

package com.gc;

import java.util.ArrayList;

import java.util.List;

/**

* 簡單的JAVA虛擬機記憶體回收,cms收集器的使用

* 參數:-Xms30m -Xmx60m -Xmn10m -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails

* @author 范芳銘

*/

public class EasyCMS {

public byte[] placeHolder = new byte[64 * 1024]; //占位符

public static void main(String[] args) throws Exception{

outOfMemoryByExpansionSize();

}

private static void outOfMemoryByExpansionSize() throws Exception{

List<EasyCMS> list = new ArrayList<EasyCMS>();

while(true){

EasyCMS serial = new EasyCMS();

list.add(serial);

Thread.sleep(10);//停頓10毫秒

}

}

}

參數

參數:-Xms30m -Xmx60m -Xmn10m -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails

-Xms30m JVM最小30M

-Xmx100m JVM最大100M

-Xmn10m 新生代固定10M

-XX:+ UseConcMarkSweepGC對應parNew + cms 收集器

-XX:+PrintGCDetails 列印詳細GC

運行結果

[GC [ParNew: 9110K->972K(9216K), 0.0056278 secs] 48708K->48702K(60416K), 0.0056664 secs] [Times: user=0.00 sys=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.003 secs] [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 (9216 K)][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.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

[CMS-concurrent-reset: 0.000/0.000 secs] [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.0185255 secs] [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.003 secs] [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 (9216 K)][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.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

[CMS-concurrent-reset: 0.000/0.000 secs] [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.0136662 secs] [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]

運作步驟

CMS收集器是基於“標記-清除”算法實現的,它的運作過程相對於前面幾種收集器來說要更複雜一些,整個過程分為6個步驟,包括:

初始標記(CMS initial mark)

並發標記(CMS concurrent mark)

並發預清理(CMS-concurrent-preclean)

重新標記(CMS remark)

並發清除(CMS concurrent sweep)

並發重置(CMS-concurrent-reset)

其中初始標記、重新標記這兩個步驟仍然需要“Stop The World”。初始標記僅僅只是標記一下GC Roots能直接關聯到的對象,速度很快,並發標記階段就是進行GC Roots Tracing的過程,而重新標記階段則是為了修正並發標記期間,因用戶程式繼續運作而導致標記產生變動的那一部分對象的標記記錄,這個階段的停頓時間一般會比初始標記階段稍長一些,但遠比並發標記的時間短。其他動作都是並發的。

對應具體的運行結果:

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

【1.初始標記】

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

【2.並發標記】

[CMS-concurrent-mark: 0.003/0.003 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

【3.並發預清理】

[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

【4.重新標記】

[GC[YG occupancy: 1036 K (9216 K)][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.並發清除】

[CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

【6.並發重置】

[CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

相關詞條

相關搜尋

熱門詞條

聯絡我們