java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
测试程序
import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
public class ManagementFactoryTest {
public final static void printGarbageCollectorMXBeansInfo(){
List<GarbageCollectorMXBean> GarbageCollectorMXBeanList=ManagementFactory.getGarbageCollectorMXBeans();
System.out.println("GarbageCollectorMXBeanList.size():"+GarbageCollectorMXBeanList.size());
for(GarbageCollectorMXBean gcMXBean:GarbageCollectorMXBeanList){
long collectionCount=gcMXBean.getCollectionCount();
long collectionTime=gcMXBean.getCollectionTime();
String[] memoryPoolNames=gcMXBean.getMemoryPoolNames();
String name=gcMXBean.getName();
String objectName=gcMXBean.getObjectName().toString();
boolean isValid=gcMXBean.isValid();
StringBuilder sb=new StringBuilder();
sb.append("collectionCount:").append(collectionCount).append("\n")
.append("collectionTime:").append(collectionTime).append("\n")
.append("GarbageCollectorName:").append(name).append("\n")
.append("objectName:").append(objectName).append("\n")
.append("isValid:").append(isValid).append("\n")
.append("memoryPoolNames:\n");
for(int i=0;i<memoryPoolNames.length;i++){
sb.append("\tmemoryPoolName_").append(i).append(":").append(memoryPoolNames[i]).append("\n");
}
System.out.println(sb.toString());
}
}
public static void main(String[] args) {
printGarbageCollectorMXBeansInfo();
}
}
测试结果vm参数及结果:
1、-XX:+UseParNewGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:ParNew
objectName:java.lang:type=GarbageCollector,name=ParNew
isValid:true
memoryPoolNames:
memoryPoolName_0:Par Eden Space
memoryPoolName_1:Par Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:MarkSweepCompact
objectName:java.lang:type=GarbageCollector,name=MarkSweepCompact
isValid:true
memoryPoolNames:
memoryPoolName_0:Par Eden Space
memoryPoolName_1:Par Survivor Space
memoryPoolName_2:Tenured GenVM option '+UseParNewGC'
VM option '+PrintVMOptions'
2、-XX:+UseConcMarkSweepGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:ParNew
objectName:java.lang:type=GarbageCollector,name=ParNew
isValid:true
memoryPoolNames:
memoryPoolName_0:Par Eden Space
memoryPoolName_1:Par Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:ConcurrentMarkSweep
objectName:java.lang:type=GarbageCollector,name=ConcurrentMarkSweep
isValid:true
memoryPoolNames:
memoryPoolName_0:Par Eden Space
memoryPoolName_1:Par Survivor Space
memoryPoolName_2:CMS Old GenVM option '+UseConcMarkSweepGC'
VM option '+PrintVMOptions'
3、-XX:+UseG1GC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:G1 Young Generation
objectName:java.lang:type=GarbageCollector,name=G1 Young Generation
isValid:true
memoryPoolNames:
memoryPoolName_0:G1 Eden Space
memoryPoolName_1:G1 Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:G1 Old Generation
objectName:java.lang:type=GarbageCollector,name=G1 Old Generation
isValid:true
memoryPoolNames:
memoryPoolName_0:G1 Eden Space
memoryPoolName_1:G1 Survivor Space
memoryPoolName_2:G1 Old GenVM option '+UseG1GC'
VM option '+PrintVMOptions'
4、-XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:PS Scavenge
objectName:java.lang:type=GarbageCollector,name=PS Scavenge
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:PS MarkSweep
objectName:java.lang:type=GarbageCollector,name=PS MarkSweep
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor Space
memoryPoolName_2:PS Old GenVM option '+UseAdaptiveSizePolicyWithSystemGC'
VM option '+PrintVMOptions'
5、-XX:+UseMaximumCompactionOnSystemGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:PS Scavenge
objectName:java.lang:type=GarbageCollector,name=PS Scavenge
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:PS MarkSweep
objectName:java.lang:type=GarbageCollector,name=PS MarkSweep
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor Space
memoryPoolName_2:PS Old GenVM option '+UseMaximumCompactionOnSystemGC'
VM option '+PrintVMOptions'
6、-XX:+UseParallelGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:PS Scavenge
objectName:java.lang:type=GarbageCollector,name=PS Scavenge
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:PS MarkSweep
objectName:java.lang:type=GarbageCollector,name=PS MarkSweep
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor Space
memoryPoolName_2:PS Old GenVM option '+UseParallelGC'
VM option '+PrintVMOptions'
7、-XX:+UseParallelOldGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:PS Scavenge
objectName:java.lang:type=GarbageCollector,name=PS Scavenge
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:PS MarkSweep
objectName:java.lang:type=GarbageCollector,name=PS MarkSweep
isValid:true
memoryPoolNames:
memoryPoolName_0:PS Eden Space
memoryPoolName_1:PS Survivor Space
memoryPoolName_2:PS Old GenVM option '+UseParallelOldGC'
VM option '+PrintVMOptions'
8、-XX:+UseSerialGC -XX:+PrintVMOptions
GarbageCollectorMXBeanList.size():2
collectionCount:0
collectionTime:0
GarbageCollectorName:Copy
objectName:java.lang:type=GarbageCollector,name=Copy
isValid:true
memoryPoolNames:
memoryPoolName_0:Eden Space
memoryPoolName_1:Survivor SpacecollectionCount:0
collectionTime:0
GarbageCollectorName:MarkSweepCompact
objectName:java.lang:type=GarbageCollector,name=MarkSweepCompact
isValid:true
memoryPoolNames:
memoryPoolName_0:Eden Space
memoryPoolName_1:Survivor Space
memoryPoolName_2:Tenured GenVM option '+UseSerialGC'
VM option '+PrintVMOptions'
9、-XX:+UseShenandoahGC -XX:+PrintVMOptions
VM option '+UseShenandoahGC'
VM option '+PrintVMOptions'
Unrecognized VM option 'UseShenandoahGC'