MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
//获取jvm堆相关的信息
// jvm 默认 newRatio = 2 survivorRatio=1 , Xmn 优先级高于 newRatio
// Heap Memory 不包括 Perm 区 ,maxMemory 要减去 1个survivorMemory
ObjectName memory = new ObjectName("java.lang:type=Memory");
CompositeData heapMemoryUsage = (CompositeData) mBeanServer.getAttribute(memory, "HeapMemoryUsage");
Long initBytes = (Long) heapMemoryUsage.get("init");
Long maxBytes = (Long) heapMemoryUsage.get("max");
Long commtedBytes = (Long) heapMemoryUsage.get("committed");
Long usedBytes = (Long) heapMemoryUsage.get("used");
System.out.println("JVM Heap Memory (m) : init=" + initBytes / 1024/1024 + " max=" + maxBytes / 1024/1024 + " commited=" + commtedBytes / 1024/1024
+ " used="+usedBytes/1024/1024);
CompositeData nonHeapMemoryUsage = (CompositeData) mBeanServer.getAttribute(memory, "NonHeapMemoryUsage");
Long initBytes1 = (Long) nonHeapMemoryUsage.get("init");
Long maxBytes1 = (Long) nonHeapMemoryUsage.get("max");
Long commtedBytes1 = (Long) nonHeapMemoryUsage.get("committed");
Long usedBytes1 = (Long) nonHeapMemoryUsage.get("used");
System.out.println("JVM NonHeap Memory (m) : init=" + initBytes1 / 1024/1024 + " max=" + maxBytes1 / 1024/1024 + " commited=" + commtedBytes1 / 1024/1024
+ " used1="+usedBytes/1024/1024);
createBlockThread();
createWaitingThread();
//获取jvm线程相关的信息
ObjectName threading = new ObjectName("java.lang:type=Threading");
Integer threadCount = (Integer) mBeanServer.getAttribute(threading, "ThreadCount");
Integer daemonThreadCount = (Integer) mBeanServer.getAttribute(threading, "DaemonThreadCount");
Long totalStartedThreadCount = (Long) mBeanServer.getAttribute(threading, "TotalStartedThreadCount");
Integer peakThreadCount = (Integer) mBeanServer.getAttribute(threading, "PeakThreadCount");
System.out.println("JVM thread statistics : threadCount="+threadCount+" daemonThreadCount="+daemonThreadCount
+" totalStartedThreadCount="+totalStartedThreadCount+" peakThreadCount="+peakThreadCount +" blockedCount="+getBLockedThreadCount());
ObjectName os = new ObjectName("java.lang:type=OperatingSystem");
Long processCpuTime=(Long) mBeanServer.getAttribute(os,"ProcessCpuTime");
System.out.println("JVM processCpuTime= "+(processCpuTime/(1000000*1000)));
System.out.println("system time : "+System.currentTimeMillis());
ObjectName garbage = new ObjectName("java.lang:type=GarbageCollector,name=ParNew");
Long collectionCount=(Long) mBeanServer.getAttribute(garbage,"CollectionCount");
Long collectionTime=(Long) mBeanServer.getAttribute(garbage,"CollectionTime");
System.out.println("JVM ParNew collectionCount="+collectionCount+" collectionTime="+collectionTime);
ObjectName cmsGarbage = new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep");
Long cmsCollectionCount=(Long) mBeanServer.getAttribute(cmsGarbage,"CollectionCount");
Long cmsCollectionTime=(Long) mBeanServer.getAttribute(cmsGarbage,"CollectionTime");
System.out.println("JVM CMS cmsCollectionCount="+cmsCollectionCount+" cmsCollectionTime="+cmsCollectionTime);
网友评论