Java代码中获取JVM参数

参考:http://cn-done.iteye.com/blog/2041971

package com.wjz.demo;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.List;

public class JVMArgs {

    public static void main(String[] args) {
        MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
        System.out.println("堆内存信息: " + memorymbean.getHeapMemoryUsage());   
        System.out.println("方法区内存信息: " + memorymbean.getNonHeapMemoryUsage()); 
        
        List<String> inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
        System.out.println("
#####################运行时设置的JVM参数#######################");
        System.out.println(inputArgs);
        
        System.out.println("
#####################运行时内存情况#######################");
        long totle = Runtime.getRuntime().totalMemory();
        System.out.println("总的内存量 [" + totle + "]");
        long free = Runtime.getRuntime().freeMemory();
        System.out.println("空闲的内存量 [" + free + "]");
        long max = Runtime.getRuntime().maxMemory();
        System.out.println("最大的内存量 [" + max + "]");
    }
}

输出结果

堆内存信息: init = 20971520(20480K) used = 857216(837K) committed = 20119552(19648K) max = 20119552(19648K)
方法区内存信息: init = 24313856(23744K) used = 3082720(3010K) committed = 24313856(23744K) max = 136314880(133120K)

#####################运行时设置的JVM参数#######################
[-Xmx20M, -Dfile.encoding=UTF-8]

#####################运行时内存情况#######################
总的内存量 [20119552]
空闲的内存量 [19262336]
最大的内存量 [20119552]
原文地址:https://www.cnblogs.com/BINGJJFLY/p/7610431.html