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)";
    }
}

 

posted @ 2020-04-01 11:00  淘来的星湖  阅读(290)  评论(0编辑  收藏  举报