查看/设置JVM使用的垃圾收集器

一、设置垃圾收集器的参数

  -XX:+UseSerialGC,虚拟机在Client模式下的默认值,Serial+Serial Old

  -XX:+UseParNewGC,ParNew+Serial Old,在JDK1.8中已经不推荐使用并且将被移除(Java HotSpot(TM) Client VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release)。

  -XX:+UseConcMarkSweepGC,ParNew+CMS+Serial Old(CMS执行失败时会切换到Serial Old)

  -XX:+UseParallelGC ,虚拟机在Server模式下的默认值,Parallel Scavenge+Serial Old

  -XX:+UseParallelOldGC ,Parallel Scavenge+Parallel Old

  -XX:+UseG1GC,G1 Young Generation+G1 Old Generation

二、使用Java代码测试

public class GCTest {
public static void main(String[] args) {
//-XX:+UseParallelOldGC和-XX:+UseParallelGC结果一样,因为MXBean名字一样,但是实际使用的不一样
List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean gc:garbageCollectorMXBeans){
System.out.println(gc.getName());
System.out.println("--");
}
}
}

我这里使用JDK1.8测试,启动时输入JVM参数-XX:+PrintCommandLineFlags,该参数会打印JVM默认的优化参数。

测试结果:

  1、指定参数-XX:+PrintCommandLineFlags  -XX:+UseSerialGC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlags -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
Copy
--
MarkSweepCompact
--

  2、指定参数-XX:+PrintCommandLineFlags  -XX:+UseParNewGC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlags -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC 
ParNew
--
MarkSweepCompact
--
Java HotSpot(TM) Client VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release

  3、指定参数-XX:+PrintCommandLineFlags  -XX:+UseConcMarkSweepGC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:MaxNewSize=89481216 -XX:MaxTenuringThreshold=6 -XX:OldPLABSize=16 -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC 
ParNew
--
ConcurrentMarkSweep
--

  4、指定参数-XX:+PrintCommandLineFlags  -XX:+UseParallelGC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlags -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
PS Scavenge
--
PS MarkSweep
--

  5、指定参数-XX:+PrintCommandLineFlags  -XX:+UseParallelOldGC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlags -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelOldGC 
PS Scavenge
--
PS MarkSweep
--

  6、指定参数-XX:+PrintCommandLineFlags  -XX:+UseG1GC

-XX:InitialHeapSize=16777216 -XX:MaxHeapSize=268435456 -XX:+PrintCommandLineFlags -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation 
G1 Young Generation
--
G1 Old Generation
--

三、使用内置JConsle查看

  1、写一个一直运行的代码

public class GCTest {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Thread.sleep(1000); 
        }
    }
}   

  2、打开命令行,输入jconsole

  3、显示连接的Java程序

  4、查看VM概要

  5、也可以在MBean中查看

四、总结

  -XX:+UseParallelGC和-XX:+UseParallelOldGC除了JVM参数不同,打印结果都一样,是因为设置了相同的MXBean名称,具体可以看一下这一篇文章:https://www.cnblogs.com/kelthuzadx/p/10924117.html

posted @ 2019-12-07 19:50  卫旗  阅读(2511)  评论(0编辑  收藏  举报