0
点赞
收藏
分享

微信扫一扫

计算一个对象占用的内存

ZSACH 2022-08-17 阅读 57

/**
* 计算一个MAP对象占用的内存
* @param args
*/
public static void main(String[] args) {
System.gc();
long total = Runtime.getRuntime().totalMemory(); //单位:byte
long m1 = Runtime.getRuntime().freeMemory();
System.out.println("total:" + total);
System.out.println("before:" + (total - m1));

Map<Object,Object> map = new HashMap<Object,Object>();
for(int i=0; i < 400; i++){
map.put(new Object(),new Object());
}
long total1 = Runtime.getRuntime().totalMemory();
long m2 = Runtime.getRuntime().freeMemory();
System.out.println("after:" + (total1 - m2));
System.out.println(map.toString());
}

举报

相关推荐

0 条评论