java动态获取jvm参数

JDK提供java.lang.management包, 其实就是基于JMX技术规范,提供一套完整的MBean,动态获取JVM的运行时数据,达到监控JVM性能的目的。
java.lang.management包,是Java SE 5 中新引入的 JMX API。

package com.ldz.jvm;

import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.List;
public class JVMMXBeanDemo {
    /** * @param args */
    public static void main(String[] args) {
        //==========================Memory=========================
        System.out.println("==========================Memory=========================");
        MemoryMXBean memoryMBean = ManagementFactory.getMemoryMXBean(); 
        MemoryUsage usage = memoryMBean.getHeapMemoryUsage(); 
        System.out.println("初始化 Heap: " + (usage.getInit()/1024/1024) + "mb"); 
        System.out.println("最大Heap: " + (usage.getMax()/1024/1024) + "mb"); 
        System.out.println("已经使用Heap: " + (usage.getUsed()/1024/1024) + "mb"); 
        System.out.println("Heap Memory Usage: " + memoryMBean.getHeapMemoryUsage()); 
        System.out.println("Non-Heap Memory Usage: " + memoryMBean.getNonHeapMemoryUsage()); 
        //==========================Runtime=========================
        System.out.println("==========================Runtime=========================");
        RuntimeMXBean runtimeMBean = ManagementFactory.getRuntimeMXBean();
        System.out.println("JVM name : " + runtimeMBean.getVmName());
        System.out.println("lib path : " + runtimeMBean.getLibraryPath());
        System.out.println("class path : " + runtimeMBean.getClassPath());
        System.out.println("getVmVersion() " + runtimeMBean.getVmVersion()); 
        //java options
        List<String> argList = runtimeMBean.getInputArguments();
        for(String arg : argList){
            System.out.println("arg : " + arg);
        }
        //==========================OperatingSystem=========================
        System.out.println("==========================OperatingSystem=========================");
        OperatingSystemMXBean osMBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); 
        //获取操作系统相关信息  
        System.out.println("getName() "+ osMBean.getName()); 
        System.out.println("getVersion() " + osMBean.getVersion()); 
        System.out.println("getArch() "+osMBean.getArch()); 
        System.out.println("getAvailableProcessors() " + osMBean.getAvailableProcessors()); 
        //==========================Thread=========================
        System.out.println("==========================Thread=========================");
        //获取各个线程的各种状态,CPU 占用情况,以及整个系统中的线程状况  
        ThreadMXBean threadMBean=(ThreadMXBean)ManagementFactory.getThreadMXBean(); 
        System.out.println("getThreadCount() " + threadMBean.getThreadCount()); 
        System.out.println("getPeakThreadCount() " + threadMBean.getPeakThreadCount()); 
        System.out.println("getCurrentThreadCpuTime() " + threadMBean.getCurrentThreadCpuTime()); 
        System.out.println("getDaemonThreadCount() " + threadMBean.getDaemonThreadCount()); 
        System.out.println("getCurrentThreadUserTime() "+ threadMBean.getCurrentThreadUserTime()); 
        //==========================Compilation=========================
        System.out.println("==========================Compilation=========================");
        CompilationMXBean compilMBean=(CompilationMXBean)ManagementFactory.getCompilationMXBean(); 
        System.out.println("getName() " + compilMBean.getName()); 
        System.out.println("getTotalCompilationTime() " + compilMBean.getTotalCompilationTime()); 
        //==========================MemoryPool=========================
        System.out.println("==========================MemoryPool=========================");
        //获取多个内存池的使用情况  
        List<MemoryPoolMXBean> mpMBeanList= ManagementFactory.getMemoryPoolMXBeans(); 
        for(MemoryPoolMXBean mpMBean : mpMBeanList){  
            System.out.println("getUsage() " + mpMBean.getUsage()); 
            System.out.println("getMemoryManagerNames() "+ mpMBean.getMemoryManagerNames().toString()); 
        } 
        //==========================GarbageCollector=========================
        System.out.println("==========================GarbageCollector=========================");
        //获取GC的次数以及花费时间之类的信息  
        List<GarbageCollectorMXBean> gcMBeanList=ManagementFactory.getGarbageCollectorMXBeans(); 
        for(GarbageCollectorMXBean gcMBean : gcMBeanList){  
            System.out.println("getName() " + gcMBean.getName()); 
            System.out.println("getMemoryPoolNames() "+ gcMBean.getMemoryPoolNames()); 
        } 
        //==========================Other=========================
        System.out.println("==========================Other=========================");
        //Java 虚拟机中的内存总量,以字节为单位  
        int total = (int)Runtime.getRuntime().totalMemory()/1024/1024;
        System.out.println("内存总量 :" + total + "mb"); 
        int free = (int)Runtime.getRuntime().freeMemory()/1024/1024; 
        System.out.println("空闲内存量 : " + free + "mb"); 
        int max = (int) (Runtime.getRuntime().maxMemory() /1024 / 1024); 
        System.out.println("最大内存量 : "  + max + "mb"); 

    }
}

输出结果:
==========================Memory=========================
初始化 Heap: 16mb
最大Heap: 247mb
已经使用Heap: 0mb
Heap Memory Usage: init = 16777216(16384K) used = 972640(949K) committed = 16252928(15872K) max = 259522560(253440K)
Non-Heap Memory Usage: init = 163840(160K) used = 2474752(2416K) committed = 3145728(3072K) max = -1(-1K)
==========================Runtime=========================
JVM name : Java HotSpot(TM) Client VM
lib path : C:\Program Files\Java\jdk1.8.0_60\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_60/bin/client;C:/Program Files/Java/jre1.8.0_60/bin;C:/Program Files/Java/jre1.8.0_60/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\apache-maven-3.3.3\bin;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;D:\eclipse;;.
class path : D:\workspace\test\target\test-classes;D:\workspace\test\target\classes;C:\Users\Administrator.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\Administrator.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-core\4.0.2.RELEASE\spring-core-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-web\4.0.2.RELEASE\spring-web-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-beans\4.0.2.RELEASE\spring-beans-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-context\4.0.2.RELEASE\spring-context-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-oxm\4.0.2.RELEASE\spring-oxm-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-tx\4.0.2.RELEASE\spring-tx-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-jdbc\4.0.2.RELEASE\spring-jdbc-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-webmvc\4.0.2.RELEASE\spring-webmvc-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-expression\4.0.2.RELEASE\spring-expression-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-aop\4.0.2.RELEASE\spring-aop-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-context-support\4.0.2.RELEASE\spring-context-support-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\springframework\spring-test\4.0.2.RELEASE\spring-test-4.0.2.RELEASE.jar;C:\Users\Administrator.m2\repository\org\mybatis\mybatis\3.2.6\mybatis-3.2.6.jar;C:\Users\Administrator.m2\repository\org\mybatis\mybatis-spring\1.2.2\mybatis-spring-1.2.2.jar;C:\Users\Administrator.m2\repository\javax\javaee-api\7.0\javaee-api-7.0.jar;C:\Users\Administrator.m2\repository\com\sun\mail\javax.mail\1.5.0\javax.mail-1.5.0.jar;C:\Users\Administrator.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\Administrator.m2\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar;C:\Users\Administrator.m2\repository\commons-dbcp\commons-dbcp\1.2.2\commons-dbcp-1.2.2.jar;C:\Users\Administrator.m2\repository\commons-pool\commons-pool\1.3\commons-pool-1.3.jar;C:\Users\Administrator.m2\repository\jstl\jstl\1.2\jstl-1.2.jar;C:\Users\Administrator.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator.m2\repository\com\alibaba\fastjson\1.1.41\fastjson-1.1.41.jar;C:\Users\Administrator.m2\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;C:\Users\Administrator.m2\repository\org\slf4j\slf4j-log4j12\1.7.7\slf4j-log4j12-1.7.7.jar;C:\Users\Administrator.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.13\jackson-mapper-asl-1.9.13.jar;C:\Users\Administrator.m2\repository\org\codehaus\jackson\jackson-core-asl\1.9.13\jackson-core-asl-1.9.13.jar;C:\Users\Administrator.m2\repository\commons-fileupload\commons-fileupload\1.3.1\commons-fileupload-1.3.1.jar;C:\Users\Administrator.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\Administrator.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar
getVmVersion() 25.60-b23
arg : -Dfile.encoding=UTF-8
==========================OperatingSystem=========================
getName() Windows 7
getVersion() 6.1
getArch() x86
getAvailableProcessors() 3
==========================Thread=========================
getThreadCount() 5
getPeakThreadCount() 5
getCurrentThreadCpuTime() 140400900
getDaemonThreadCount() 4
getCurrentThreadUserTime() 78000500
==========================Compilation=========================
getName() HotSpot Client Compiler
getTotalCompilationTime() 9
==========================MemoryPool=========================
getUsage() init = 163840(160K) used = 701888(685K) committed = 720896(704K) max = 33554432(32768K)
getMemoryManagerNames() [Ljava.lang.String;@139a55
getUsage() init = 0(0K) used = 1848776(1805K) committed = 2424832(2368K) max = -1(-1K)
getMemoryManagerNames() [Ljava.lang.String;@1db9742
getUsage() init = 4521984(4416K) used = 972640(949K) committed = 4521984(4416K) max = 71630848(69952K)
getMemoryManagerNames() [Ljava.lang.String;@106d69c
getUsage() init = 524288(512K) used = 0(0K) committed = 524288(512K) max = 8912896(8704K)
getMemoryManagerNames() [Ljava.lang.String;@52e922
getUsage() init = 11206656(10944K) used = 0(0K) committed = 11206656(10944K) max = 178978816(174784K)
getMemoryManagerNames() [Ljava.lang.String;@25154f
==========================GarbageCollector=========================
getName() Copy
getMemoryPoolNames() [Ljava.lang.String;@10dea4e
getName() MarkSweepCompact
getMemoryPoolNames() [Ljava.lang.String;@647e05
==========================Other=========================
内存总量 :15mb
空闲内存量 : 14mb
最大内存量 : 247mb

参考博文:http://www.what21.com/programming/java/java-summary/java-jvm-args.html

你可能感兴趣的:(jvm)