一. 查看默认配置
1.cmd输入命令
java -XX:+PrintCommandLineFlags -version
输出结果如下
-XX:InitialHeapSize=266189632 -XX:MaxHeapSize=4259034112
-XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers
-XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation
-XX:+UseParallelGC
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
默认使用的垃圾收集器配置参数为ParallelGC, 那到底是什么收集器组合呢?
二.分析验证
《深入理解Java虚拟机》第三版第3.7.4节中说明如下:
UseParallelGC是使用ParallelScavenge+SerialOld收集器组合
UseParallelOldGC是使用ParallelScavenge+ParallelOld收集器组合
下面进行分析验证
1.获取垃圾收集器
public class GCTest {
public static void main(String[] args) {
List<GarbageCollectorMXBean> list = ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean bean: list) {
System.out.println(bean.getName());
}
}
}
2.JDK8验证
设置VM参数 -XX:+UseParallelGC -verbose:gc -XX:+PrintGCDetails
输出如下:
PS Scavenge
PS MarkSweep
Heap
PSYoungGen total 75776K, used 11719K [0x000000076b600000, 0x0000000770a80000, 0x00000007c0000000)
eden space 65024K, 18% used [0x000000076b600000,0x000000076c171c30,0x000000076f580000)
from space 10752K, 0% used [0x0000000770000000,0x0000000770000000,0x0000000770a80000)
to space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
ParOldGen total 173568K, used 0K [0x00000006c2200000, 0x00000006ccb80000, 0x000000076b600000)
object space 173568K, 0% used [0x00000006c2200000,0x00000006c2200000,0x00000006ccb80000)
Metaspace used 3644K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 397K, capacity 428K, committed 512K, reserved 1048576K
设置VM参数 -XX:+UseParallelOldGC -verbose:gc -XX:+PrintGCDetails
输出如下:
PS Scavenge
PS MarkSweep
Heap
PSYoungGen total 75776K, used 11719K [0x000000076b600000, 0x0000000770a80000, 0x00000007c0000000)
eden space 65024K, 18% used [0x000000076b600000,0x000000076c171c30,0x000000076f580000)
from space 10752K, 0% used [0x0000000770000000,0x0000000770000000,0x0000000770a80000)
to space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
ParOldGen total 173568K, used 0K [0x00000006c2200000, 0x00000006ccb80000, 0x000000076b600000)
object space 173568K, 0% used [0x00000006c2200000,0x00000006c2200000,0x00000006ccb80000)
Metaspace used 3631K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 395K, capacity 428K, committed 512K, reserved 1048576K
似乎UseParallelGC和UseParallelOldGC没什么区别
都是ParallelScavenge+ParallelOld组合
3.JDK6验证
设置VM参数 -XX:+UseParallelGC -verbose:gc -XX:+PrintGCDetails
输出如下:
PS Scavenge
PS MarkSweep
Heap
PSYoungGen total 75840K, used 3901K [0x00000007ab600000, 0x00000007b0aa0000, 0x0000000800000000)
eden space 65024K, 6% used [0x00000007ab600000,0x00000007ab9cf658,0x00000007af580000)
from space 10816K, 0% used [0x00000007b0010000,0x00000007b0010000,0x00000007b0aa0000)
to space 10816K, 0% used [0x00000007af580000,0x00000007af580000,0x00000007b0010000)
PSOldGen total 173312K, used 0K [0x0000000702200000, 0x000000070cb40000, 0x00000007ab600000)
object space 173312K, 0% used [0x0000000702200000,0x0000000702200000,0x000000070cb40000)
PSPermGen total 21248K, used 3592K [0x00000006fd000000, 0x00000006fe4c0000, 0x0000000702200000)
object space 21248K, 16% used [0x00000006fd000000,0x00000006fd383310,0x00000006fe4c0000)
设置VM参数 -XX:+UseParallelOldGC -verbose:gc -XX:+PrintGCDetails
输出如下:
PS Scavenge
PS MarkSweep
Heap
PSYoungGen total 75840K, used 3901K [0x00000007ab600000, 0x00000007b0aa0000, 0x0000000800000000)
eden space 65024K, 6% used [0x00000007ab600000,0x00000007ab9cf658,0x00000007af580000)
from space 10816K, 0% used [0x00000007b0010000,0x00000007b0010000,0x00000007b0aa0000)
to space 10816K, 0% used [0x00000007af580000,0x00000007af580000,0x00000007b0010000)
ParOldGen total 173312K, used 0K [0x0000000702200000, 0x000000070cb40000, 0x00000007ab600000)
object space 173312K, 0% used [0x0000000702200000,0x0000000702200000,0x000000070cb40000)
PSPermGen total 21248K, used 3600K [0x00000006fd000000, 0x00000006fe4c0000, 0x0000000702200000)
object space 21248K, 16% used [0x00000006fd000000,0x00000006fd3840f0,0x00000006fe4c0000)
这时可以看出区别, UseParallelGC是使用ParallelScavenge+SerialOld收集器组合
UseParallelOldGC是使用ParallelScavenge+ParallelOld收集器组合
4.总结
a.PS MarkSweep只是回收器的别名, 可指代SerialOld和ParallelOld
b.有高手在JDK源码commit记录里面找到了答案
在JDK 7U4之前UserParallelGC用的是ParallelScavenge+SerialOld,在这个版本后Parallel已经很成熟了,所以直接替换了旧的收集器,所以JDK 7u4后的7和JDK8默认使用的都是ParallelScavenge+ParallelOld
Server-class machine ergonomics was introduced in jdk5. If the machine upon which
the jvm is running is powerful enough (currently, at least 2 physical cores plus
at least 2gb of memory), the server jvm is invoked using the parallel scavenger
rather than the serial scavenger. Currently the old gen collector used is
serial mark-sweep-compact. Now that the parallel old gen collector is mature,
we should change to using it instead.
c.当看书或其他途径获取知识时, 遇到知识点矛盾, 一定要静下心多尝试验证, 避免一知半解, 或只是死记硬背一个观点, 此乃大忌!