通过命令行的参数可以指定JVM的GC收集器,本文总结一下常见GC收集器的指定参数,供参考。

-XX:+UseParallelGC

Server模式下默认开启,GC日志输出如下:

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails  messageSenderTest.JVMTest3
 2[Full GC (Ergonomics) [PSYoungGen: 648K->0K(9216K)] [ParOldGen: 6152K->6689K(10240K)] 6800K->6689K(19456K), [Metaspace: 2665K->2665K(1056768K)], 0.0042451 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 3Heap
 4 PSYoungGen      total 9216K, used 2130K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 5  eden space 8192K, 26% used [0x00000000ff600000,0x00000000ff814930,0x00000000ffe00000)
 6  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 7  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 8 ParOldGen       total 10240K, used 6689K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
 9  object space 10240K, 65% used [0x00000000fec00000,0x00000000ff288510,0x00000000ff600000)
10 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
11  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

PSYoungGen:其中PS是Parallel Scavenge的缩写,表示新生代使用了Parallel Scavenge收集器。

ParOldGen:其中ParOld是Parallel Old的简写,表示老年代使用了Parallel Old收集器。

-XX:+UseSerialGC

Client模式下默认开启。GC日志输出如下:

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC messageSenderTest.JVMTest3
 2[GC (Allocation Failure) [DefNew: 7129K->546K(9216K), 0.0032658 secs] 7129K->6690K(19456K), 0.0032931 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 3[GC (Allocation Failure) [DefNew: 6850K->6850K(9216K), 0.0000074 secs][Tenured: 6144K->8192K(10240K), 0.0024199 secs] 12994K->12832K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0024507 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 4Heap
 5 def new generation   total 9216K, used 6852K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
 6  eden space 8192K,  83% used [0x00000000fec00000, 0x00000000ff2b1210, 0x00000000ff400000)
 7  from space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 8  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 9 tenured generation   total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
10   the space 10240K,  80% used [0x00000000ff600000, 0x00000000ffe00040, 0x00000000ffe00200, 0x0000000100000000)
11 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
12  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

DefNew:新生代区域,使用的是Serial收集器,单线程,复制算法

Tenured: 老年代区域,使用的是Serial Old收集器,单线程,标记整理算法

-XX:+UseParNewGC

新生代ParNew,老年代Serial Old,其中ParNew是Serial的多线程版本,算法原理都一样。GC日志如下:

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails -XX:+UseParNewGC messageSenderTest.JVMTest3
 2[GC (Allocation Failure) [ParNew: 7129K->577K(9216K), 0.0026304 secs] 7129K->6721K(19456K), 0.0027019 secs] [Times: user=0.06 sys=0.00, real=0.00 secs]
 3[GC (Allocation Failure) [ParNew: 6881K->6881K(9216K), 0.0000231 secs][Tenured: 6144K->8192K(10240K), 0.0038952 secs] 13025K->12832K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0039455 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 4Heap
 5 par new generation   total 9216K, used 6852K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
 6  eden space 8192K,  83% used [0x00000000fec00000, 0x00000000ff2b1210, 0x00000000ff400000)
 7  from space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 8  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 9 tenured generation   total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
10   the space 10240K,  80% used [0x00000000ff600000, 0x00000000ffe00040, 0x00000000ffe00200, 0x0000000100000000)
11 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
12  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
13Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release

ParNew:新生代区域,使用ParNew收集器

Tenured: 老年代区域,使用的是Serial Old收集器

日志中提示信息很明显了,对于Java HotSpot(TM) 64-Bit Server VM,使用ParNew + Serial Old这种组合是不推荐的,可能会在后续的正式版本中移除掉。

-XX:+UseParallelOldGC

与-XX:+UseParallelGC这个参数一样,使用的收集器:Parallel Scavenge + Parallel Old

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails -XX:+UseParallelOldGC messageSenderTest.JVMTest3
 2[GC (Allocation Failure) [PSYoungGen: 7129K->664K(9216K)] 7129K->6816K(19456K), 0.0022409 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 3[Full GC (Ergonomics) [PSYoungGen: 664K->0K(9216K)] [ParOldGen: 6152K->6689K(10240K)] 6816K->6689K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0048171 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
 4[Full GC (Ergonomics) [PSYoungGen: 6304K->4096K(9216K)] [ParOldGen: 6689K->8736K(10240K)] 12993K->12832K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0042737 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 5Heap
 6 PSYoungGen      total 9216K, used 6308K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 7  eden space 8192K, 77% used [0x00000000ff600000,0x00000000ffc29100,0x00000000ffe00000)
 8  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 9  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
10 ParOldGen       total 10240K, used 8736K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
11  object space 10240K, 85% used [0x00000000fec00000,0x00000000ff488150,0x00000000ff600000)
12 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
13  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

-XX:+UseParallelOldGC

老年代显式禁用Parallel Old收集器,退回到Serial Old。新生代依然是Parallel Scavenge.

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails -XX:-UseParallelOldGC messageSenderTest.JVMTest3
 2[GC (Allocation Failure) [PSYoungGen: 7129K->648K(9216K)] 7129K->6800K(19456K), 0.0026454 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 3[Full GC (Ergonomics) [PSYoungGen: 648K->0K(9216K)] [PSOldGen: 6152K->6696K(10240K)] 6800K->6696K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0020267 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 4[Full GC (Ergonomics) [PSYoungGen: 6304K->4096K(9216K)] [PSOldGen: 6696K->8744K(10240K)] 13000K->12840K(19456K), [Metaspace: 2666K->2666K(1056768K)], 0.0022958 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 5Heap
 6 PSYoungGen      total 9216K, used 6308K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 7  eden space 8192K, 77% used [0x00000000ff600000,0x00000000ffc29100,0x00000000ffe00000)
 8  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 9  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
10 PSOldGen        total 10240K, used 8744K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
11  object space 10240K, 85% used [0x00000000fec00000,0x00000000ff48a048,0x00000000ff600000)
12 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
13  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

-XX:+UseConcMarkSweepGC

表示老年代要使用CMS(concurrent mark-sweep)收集器,相应地年轻代使用ParNew。GC日志如下:

 1$ java -Xms20m -Xmx20m -Xmn10m -verbose:gc -XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+PrintCommandLineFlags messageSenderTest.JVMTest3
 2-XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=10485760 -XX:MaxTenuringThreshold=6 -XX:NewSize=10485760 -XX:OldPLABSize=16 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
 3[GC (Allocation Failure) [ParNew: 7129K->565K(9216K), 0.0021883 secs] 7129K->6711K(19456K), 0.0022223 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 4Heap
 5 par new generation   total 9216K, used 2695K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
 6  eden space 8192K,  26% used [0x00000000fec00000, 0x00000000fee14930, 0x00000000ff400000)
 7  from space 1024K,  55% used [0x00000000ff500000, 0x00000000ff58d4f8, 0x00000000ff600000)
 8  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 9 concurrent mark-sweep generation total 10240K, used 6146K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
10 Metaspace       used 2672K, capacity 4486K, committed 4864K, reserved 1056768K
11  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K

par new generation: 年轻代

concurrent mark-sweep generation: 老年代

从日志中也可以看出,在命令行参数中增加-XX:+PrintCommandLineFlags打印出默认参数发现,当使用-XX:+UseConcMarkSweepGC时会默认带上-XX:+UseParNewGC