JVM-----IDEA中查看JVM状态

怎么获取 Java 程序使用的内存?堆使用的百分比?

可以通过 java.lang.Runtime 类中与内存相关方法来获取剩余的内存,总内存及最大堆内存。通过这些方法你也可以获取到堆使用的百分比及堆内存的剩余空间。 Runtime.freeMemory() 方法返回剩余空间的字节数,Runtime.totalMemory() 方法总内存的字节数,Runtime.maxMemory() 返回最大内存的字节数。

java代码实现

public class jvmDemo {
    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        System.out.println("最大内存(M):"+runtime.maxMemory()/1024/1024);
        System.out.println("总内存(M):"+runtime.totalMemory()/1024/1024);
        System.out.println("可用内存(M):"+runtime.freeMemory()/1024/1024);
        System.out.println("打开计算器:"+runtime.exec("calc"));
        System.out.println("jvm可用的处理器核心的数 量:"+runtime.availableProcessors()/1024/1024);
        //通知JVM进行gc
        runtime.gc();
        System.gc();
    }
}

你可能感兴趣的:(JVM)