我很想知道HashMap到底极限容量是多少呢?搜了很久,没找到答案。也懒得继续找了~~~

反正应该很大很大~~

 

但HashMap占内存比较大,不少人都更关注于HashMap所占的内存怎么计算。

运行了一下,没找到HashMap的极限容量,但就发现HashMap很耗内存,一下子就OutOfMemoryError了。

Integer a = 1;
long start = 0;
long end = 0;
// 先垃圾回收
System.gc();
start
= Runtime.getRuntime().freeMemory();
HashMap map
= new HashMap();
for (int i = 0; i < 1000000; i++) {
map.put(i, a);
}
// 快要计算的时,再清理一次
System.gc();
end
= Runtime.getRuntime().freeMemory();
System.out.println(
"一个HashMap对象占内存:" + (end - start));

 

当添加2000000个item的时候,就内存溢出了。

1000000个item的HashMap就占内存接近60M了~~夸张

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-17
  • 2021-09-01
  • 2021-12-06
  • 2022-12-23
  • 2021-09-30
  • 2021-07-05
  • 2021-11-20
相关资源
相似解决方案