我们设置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。
算是对上述设置后的一种验证
测试环境
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
启动时增加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)
测试结果
-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
-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
-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
-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
-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
-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
-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
上面的第二、三部分使用代码的确是可以查到具体使用的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