简体   繁体   English

位集消耗更多内存

[英]Bitset consuming more memory

I have a list of Bitset having 25Million Bitset. 我有一个拥有2500万个Bitset的Bitset列表。 Each Bitset I'm creating by using: 我使用以下方法创建的每个位集:

Bitset.valueOf(new long[] {1})

The memory being consumed is around 1300MB. 消耗的内存约为1300MB。 Ie: in an average its taking 52bytes. 即:平均占用52字节。 I'm not understanding why so much of memory is being consumed. 我不明白为什么要消耗这么多的内存。

Each BitSet is itself an object instance (each reference consumes memory). 每个BitSet本身就是一个对象实例(每个引用消耗内存)。 You should see your memory usage go down dramatically if you do 如果这样做,您应该看到内存使用量急剧下降

BitSet b = new BitSet(25 * 1000 * 1000);
b.set(0, 25 * 1000 * 1000, true);

Create a heapdump and check it with some analyzer (eg Eclipse MAT). 创建一个堆转储并使用一些分析器(例如Eclipse MAT)对其进行检查。 That provides some insight of where that memory goes. 这提供了有关该内存去向的一些见解。

Since BitSets are objects and they contain values as well, you'll probably see the following (I bet I forgot something but you should get what I mean): 由于BitSet是对象,并且它们也包含值,因此您可能会看到以下内容(我敢打赌我忘了一些东西,但是您应该明白我的意思了):

  • for each BitSet you have a 8-byte reference (assuming you're using a 64-bit JVM) 对于每个BitSet,您都有一个8字节的引用(假设您使用的是64位JVM)
  • each BitSet contains a reference to a long[] array with one element, that's another 20 bytes (8 for the reference, 8 for the single value in the array, 4 for the length field of the array) 每个BitSet包含对带有一个元素的long []数组的引用,即另外20个字节(8个用于引用,8个用于数组中的单个值,4个用于数组的length字段)
  • each BitSet contains a boolean and an int, that's another 5 bytes. 每个BitSet包含一个布尔值和一个int,即另外5个字节。

Summing that up you get 33 bytes per BitSet (I'm sure I forgot something) and 25.000.000 * 33 bytes = 825000000 bytes (or around 786 MB). 总结一下,每个BitSet会得到33个字节(我确定我忘记了),而25.000.000 * 33个字节= 825000000个字节(或大约786 MB)。

25 million bits alone would need around 3 MB (eg if you'd create a BitSet that large). 仅2500万位就需要大约3 MB(例如,如果要创建那么大的BitSet)。

As you can see, a BitSet containing only one bit is a huge waste of memory. 如您所见,仅包含一位的BitSet会浪费大量内存。 If you can't use a single BitSet (I wouldn't see a reason for that though) you'd probably better off with a boolean array with a size of 25 million. 如果您不能使用单个BitSet(尽管我看不到原因),则最好使用大小为2500万的布尔数组。 That would still need around 95MB though. 不过,那仍然需要大约95MB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM