gpt4 book ai didi

Java 集合 : What happens when "size" exceeds "int"?

转载 作者:IT王子 更新时间:2023-10-28 23:35:07 24 4
gpt4 key购买 nike

我知道 Java 集合非常消耗内存,我自己做了一个测试,证明 4GB 勉强足以将几百万个 Integer 存储到 HashSet .

但是如果我有“足够”的内存呢? Collection.size() 会发生什么?

编辑: 已解决:Collection.size() 在超出整数范围时返回 Integer.MAX
新问题:那么如何确定集合中元素的“真实”数量?

注意 1:抱歉,这可能是一个让我用谷歌搜索你的问题,但我真的什么都没找到;)

注意 2:据我了解,集合的每个整数条目是:reference + cached_hashcode + boxed_integer_object + real_int_value,对吧?

注意 3:有趣的是,即使使用 JDK7 和“压缩指针”,当 JVM 使用 2GB 实际内存时,它在 VisualVM 中仅显示 1.5GB 分配的内存。

对于那些关心的人:

测试来源:

import java.util.*;
import java.lang.management.*;

public final class _BoxedValuesInSetMemoryConsumption {
private final static int MILLION = 1000 * 1000;

public static void main(String... args) {
Set<Integer> set = new HashSet<Integer>();

for (int i = 1;; ++i) {
if ((i % MILLION) == 0) {
int milsOfEntries = (i / MILLION);
long mbytes = ManagementFactory.getMemoryMXBean().
getHeapMemoryUsage().getUsed() / MILLION;
int ratio = (int) mbytes / milsOfEntries;
System.out.println(milsOfEntries + " mil, " + mbytes + " MB used, "
+ " ratio of bytes per entry: " + ratio);
}

set.add(i);
}
}
}

执行参数:

在 OpenSuse 11.3 x64 下使用 x64 版本的 JDK7 build 105 进行测试。

-XX:+UseCompressedOops -Xmx2048m

输出结果:

1 mil, 56 MB used,  ratio of bytes per entry: 56
2 mil, 113 MB used, ratio of bytes per entry: 56
3 mil, 161 MB used, ratio of bytes per entry: 53
4 mil, 225 MB used, ratio of bytes per entry: 56
5 mil, 274 MB used, ratio of bytes per entry: 54
6 mil, 322 MB used, ratio of bytes per entry: 53
7 mil, 403 MB used, ratio of bytes per entry: 57
8 mil, 452 MB used, ratio of bytes per entry: 56
9 mil, 499 MB used, ratio of bytes per entry: 55
10 mil, 548 MB used, ratio of bytes per entry: 54
11 mil, 596 MB used, ratio of bytes per entry: 54
12 mil, 644 MB used, ratio of bytes per entry: 53
13 mil, 827 MB used, ratio of bytes per entry: 63
14 mil, 874 MB used, ratio of bytes per entry: 62
15 mil, 855 MB used, ratio of bytes per entry: 57
16 mil, 902 MB used, ratio of bytes per entry: 56
17 mil, 951 MB used, ratio of bytes per entry: 55
18 mil, 999 MB used, ratio of bytes per entry: 55
19 mil, 1047 MB used, ratio of bytes per entry: 55
20 mil, 1096 MB used, ratio of bytes per entry: 54
21 mil, 1143 MB used, ratio of bytes per entry: 54
22 mil, 1191 MB used, ratio of bytes per entry: 54
23 mil, 1239 MB used, ratio of bytes per entry: 53
24 mil, 1288 MB used, ratio of bytes per entry: 53
25 mil, 1337 MB used, ratio of bytes per entry: 53
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

最后,使用了大约 2 GiB 的实际内存,而不是显示的 1.3 GiB,因此每个条目的消耗甚至大于超过 53 个字节。 p>

最佳答案

I know that Java collections are very memory-hungry, and did a test myself, proving that 4GB is barely enough to store few millions of Integers into a HashSet.

Java 堆!= 系统内存。 Java 的默认堆大小仅为 128MB。请注意,这也与 JVM 使用的内存不同。

关于您的问题:来自文档,

public int size()

Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

关于Java 集合 : What happens when "size" exceeds "int"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3547833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com