GC專題-查看JVM使用的GC

一、設置垃圾收集器參數

我們設置JVM垃圾收集器參數時,一個參數就同時指定新生代老年代各自的垃圾收集器了。

  • -XX:+UseSerialGC,虛擬機運行在Client模式下的默認值,Serial+Serial Old。

  • -XX:+UseParNewGC,ParNew+Serial Old,在JDK1.8被廢棄,在JDK1.7還可以使用。

  • -XX:+UseConcMarkSweepGC,ParNew+CMS+Serial Old。

  • -XX:+UseParallelGC,Parallel Scavenge+Serial Old(PS Mark Sweep)。該參數是jdk8默認垃圾收集器

  • -XX:+UseParallelOldGC,Parallel Scavenge+Parallel Old。

  • -XX:+UseG1GC,G1+G1。

二、用代碼查看實際的GC

算是對上述設置後的一種驗證

測試環境


java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

測試代碼

package com.alioo.gc;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class GarbageCollectorDemo {
    public static void main(String[] args) {


        List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gcbean : gcbeans) {
            System.out.println("alioolog gcbean:"+gcbean.getName());
        }
    }
}

測試結果


alioolog gcbean:PS Scavenge
alioolog gcbean:PS MarkSweep

三、使用-XX:+PrintCommandLineFlags參數查看實際使用的GC

啓動時增加JVM參數(打印GC詳細信息,打印JVM參數)
-XX:+PrintGCDetails -XX:+PrintCommandLineFlags

測試環境

java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

測試結果

  1. 不手工指定gc收集器,查看默認值

-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 

alioolog gcbean:PS Scavenge
alioolog gcbean:PS MarkSweep
Heap
 PSYoungGen      total 76288K, used 6554K [0x000000076ab00000, 0x0000000770000000, 0x00000007c0000000)
  eden space 65536K, 10% used [0x000000076ab00000,0x000000076b166900,0x000000076eb00000)
  from space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
  to   space 10752K, 0% used [0x000000076eb00000,0x000000076eb00000,0x000000076f580000)
 ParOldGen       total 175104K, used 0K [0x00000006c0000000, 0x00000006cab00000, 0x000000076ab00000)
  object space 175104K, 0% used [0x00000006c0000000,0x00000006c0000000,0x00000006cab00000)
 Metaspace       used 3299K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 336K, capacity 388K, committed 512K, reserved 1048576K

  1. 指定-XX:+UseSerialGC

-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseSerialGC 
alioolog gcbean:Copy
alioolog gcbean:MarkSweepCompact
Heap
 def new generation   total 78656K, used 6996K [0x00000006c0000000, 0x00000006c5550000, 0x0000000715550000)
  eden space 69952K,  10% used [0x00000006c0000000, 0x00000006c06d5168, 0x00000006c4450000)
  from space 8704K,   0% used [0x00000006c4450000, 0x00000006c4450000, 0x00000006c4cd0000)
  to   space 8704K,   0% used [0x00000006c4cd0000, 0x00000006c4cd0000, 0x00000006c5550000)
 tenured generation   total 174784K, used 0K [0x0000000715550000, 0x0000000720000000, 0x00000007c0000000)
   the space 174784K,   0% used [0x0000000715550000, 0x0000000715550000, 0x0000000715550200, 0x0000000720000000)
 Metaspace       used 3297K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 336K, capacity 388K, committed 512K, reserved 1048576K


  1. 指定-XX:+UseParNewGC
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParNewGC 
alioolog gcbean:ParNew
alioolog gcbean:MarkSweepCompact
Heap
 par new generation   total 78656K, used 6996K [0x00000006c0000000, 0x00000006c5550000, 0x0000000715550000)
  eden space 69952K,  10% used [0x00000006c0000000, 0x00000006c06d5168, 0x00000006c4450000)
  from space 8704K,   0% used [0x00000006c4450000, 0x00000006c4450000, 0x00000006c4cd0000)
  to   space 8704K,   0% used [0x00000006c4cd0000, 0x00000006c4cd0000, 0x00000006c5550000)
 tenured generation   total 174784K, used 0K [0x0000000715550000, 0x0000000720000000, 0x00000007c0000000)
   the space 174784K,   0% used [0x0000000715550000, 0x0000000715550000, 0x0000000715550200, 0x0000000720000000)
 Metaspace       used 3280K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 336K, capacity 388K, committed 512K, reserved 1048576K


  1. 指定-XX:+UseConcMarkSweepGC
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:MaxNewSize=697933824 -XX:MaxTenuringThreshold=6 -XX:OldPLABSize=16 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseParNewGC 
alioolog gcbean:ParNew
alioolog gcbean:ConcurrentMarkSweep
Heap
 par new generation   total 78656K, used 6996K [0x00000006c0000000, 0x00000006c5550000, 0x00000006e9990000)
  eden space 69952K,  10% used [0x00000006c0000000, 0x00000006c06d5168, 0x00000006c4450000)
  from space 8704K,   0% used [0x00000006c4450000, 0x00000006c4450000, 0x00000006c4cd0000)
  to   space 8704K,   0% used [0x00000006c4cd0000, 0x00000006c4cd0000, 0x00000006c5550000)
 concurrent mark-sweep generation total 174784K, used 0K [0x00000006e9990000, 0x00000006f4440000, 0x00000007c0000000)
 Metaspace       used 3322K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 337K, capacity 388K, committed 512K, reserved 1048576K

  1. 指定-XX:+UseParallelGC
    默認參數就是這個,加不加gc都一樣
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
alioolog gcbean:PS Scavenge
alioolog gcbean:PS MarkSweep
Heap
 PSYoungGen      total 76288K, used 6554K [0x000000076ab00000, 0x0000000770000000, 0x00000007c0000000)
  eden space 65536K, 10% used [0x000000076ab00000,0x000000076b166900,0x000000076eb00000)
  from space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
  to   space 10752K, 0% used [0x000000076eb00000,0x000000076eb00000,0x000000076f580000)
 ParOldGen       total 175104K, used 0K [0x00000006c0000000, 0x00000006cab00000, 0x000000076ab00000)
  object space 175104K, 0% used [0x00000006c0000000,0x00000006c0000000,0x00000006cab00000)
 Metaspace       used 3269K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 334K, capacity 388K, committed 512K, reserved 1048576K


  1. 指定-XX:+UseParallelOldGC
    默認參數就是這個,加不加gc都一樣
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelOldGC 
alioolog gcbean:PS Scavenge
alioolog gcbean:PS MarkSweep
Heap
 PSYoungGen      total 76288K, used 6554K [0x000000076ab00000, 0x0000000770000000, 0x00000007c0000000)
  eden space 65536K, 10% used [0x000000076ab00000,0x000000076b166900,0x000000076eb00000)
  from space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
  to   space 10752K, 0% used [0x000000076eb00000,0x000000076eb00000,0x000000076f580000)
 ParOldGen       total 175104K, used 0K [0x00000006c0000000, 0x00000006cab00000, 0x000000076ab00000)
  object space 175104K, 0% used [0x00000006c0000000,0x00000006c0000000,0x00000006cab00000)
 Metaspace       used 3251K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 328K, capacity 388K, committed 512K, reserved 1048576K

  1. 指定-XX:+UseG1GC
    默認參數就是這個,加不加gc都一樣
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC 
alioolog gcbean:G1 Young Generation
alioolog gcbean:G1 Old Generation
Heap
 garbage-first heap   total 262144K, used 3072K [0x00000006c0000000, 0x00000006c0100800, 0x00000007c0000000)
  region size 1024K, 4 young (4096K), 0 survivors (0K)
 Metaspace       used 3543K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

四、使用jinfo命令查看實際使用的GC

上面的第二、三部分使用代碼的確是可以查到具體使用的GC,但是生產環境是不允許你值入代碼後再重新啓動,打印日誌來查看的,因爲這樣你就破壞了現場
所以建議你使用jinfo命令來進行查看

測試代碼
這裏隨便一個java進程即可,爲避免進程退出太快,加入了休眠代碼

package com.alioo.gc;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class GarbageCollectorDemo {
    public static void main(String[] args) {

        List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gcbean : gcbeans) {
            System.out.println("alioolog gcbean:"+gcbean.getName());
        }


        try {
            Thread.sleep(10*60*1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

測試環境

java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

測試結果
根據輸出結果-XX:+UseParallelOldGC,結合第一部分知道新生代使用Parallel Scavenge , 老年代使用Parallel Old


jps -m
87834 GarbageCollectorDemo

jinfo -flags 87834
VM Flags:
-XX:CICompilerCount=4 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:MaxNewSize=1431306240 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelOldGC


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章