首页 > 技术文章 > Java测试当前应用所占用的内存示例

zifeiy 2018-05-22 14:38 原文

package test;

import java.util.HashMap;
import java.util.Map;

public class TestMemory {
	public static void main(String[] args) {

        System.gc();
        long total = Runtime.getRuntime().totalMemory(); // byte
        long m1 = Runtime.getRuntime().freeMemory();
        System.out.println("before:" + (total - m1));
         
        Map<Double,Double> map = new HashMap<Double,Double>();
        for(int i=0; i < 1e6; i++){
            map.put(new Double(Math.random()*1e9), new Double(Math.random()*1e10));
        }
        long total1 = Runtime.getRuntime().totalMemory();
        long m2 = Runtime.getRuntime().freeMemory();
        System.out.println("after:" + (total1 - m2));
//        System.out.println(map.toString());
        System.exit(0);
    }
}

推荐阅读