Java测试内存信息

谢谢WQ前辈,他写的,在此记录

package com.karros.test;

public class TestMemory {
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(" 内存信息 :" + toMemoryInfo());
    }

    /**
     * 获取当前 jvm 的内存信息
     *
     * @return
     */
    public static String toMemoryInfo() {

        Runtime currRuntime = Runtime.getRuntime();
        int nFreeMemory = (int) (currRuntime.freeMemory() / 1024 / 1024);
        int nTotalMemory = (int) (currRuntime.totalMemory() / 1024 / 1024);
        return nFreeMemory + "M/" + nTotalMemory + "M(free/total)";
    }
}
原文地址:https://www.cnblogs.com/tldxh/p/12611110.html