1. 程式人生 > >Java一些jvm介面

Java一些jvm介面

無意間找到了個好東西,記錄下!

1獲取JVM輸入引數

List list=ManagementFactory.getRuntimeMXBean().getInputArguments();

2.獲取當前JVM程序的PID

String name = ManagementFactory.getRuntimeMXBean().getName();

String pid = name.split(“@”)[0];

3.獲取當前系統的負載

ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()

4.獲取記憶體相關的

ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();//堆記憶體

ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()//堆外記憶體

5.獲取堆疊資訊相當於jstack

ManagementFactory.getThreadMXBean().dumpAllThreads(false, false)

ThreadInfo 裡有執行緒的資訊

進行垃圾回收監控

Jstat.main(new String[]{“-gcutil”, “-h5”,pid,”1s”});

如何獲取HotSpotDiagnosticMXBean ?

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean hotspotDiagnosticMXBean = newPlatformMXBeanProxy(server, “com.sun.management:type=HotSpotDiagnostic”, HotSpotDiagnosticMXBean.class);

//獲取young GC 和full GC 次數

List list=ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean e:list){
logger.info(“name={},count={},time={}”,e.getName(),e.getCollectionCount(),e.getCollectionTime());