问题描述
我想知道Scala中BitSet的内存使用量是多少,例如,如果这样做:
I would like to know what is the memory usage of BitSet in Scala.For example, if I do:
var bitArray:BitSet=new BitSet(10)
bitArray.add(0)
bitArray.add(2)
bitArray.add(4)
bitArray.add(6)
bitArray.add(8)
与包含偶数0、2、4、6、8的和数组比较,那又如何?
How does that compare with and array containing the even numbers 0,2,4,6,8?
用二进制写数字怎么办?
What about writing a number in binary:
var bitArray:BitSet=new BitSet(32)
bitArray.add(5)
bitArray.add(3)
bitArray.add(2)
bitArray.add(1)
bitArray.add(0)
与数字47相比如何?
我在这里询问内存使用情况.但是,作为一个更开放的问题,如果您知道BitSet的优点/缺点或用途是什么(WR写入其他常见数据类型).
I'm asking here of memory usage. But as a more open question, if you know, what are the advantages/disadvantages or uses of BitSet (WR to other common data types).
谢谢
推荐答案
您可以在此处查看Scala 2.8中BitSet的实现: scala.collection.mutable.BitSet .
You can look at the implementation of BitSet in Scala 2.8 here: scala.collection.mutable.BitSet.
它是基于Longs数组实现的.数组的大小仅取决于存储在其中的最大数目.将存储在其中的最高数字除以64,然后四舍五入,就可以得到数组的大小.数组中的每个元素消耗8个字节.
It is implemented based on an array of Longs. The size of the array depends only on the highest number stored in it. Divide the highest number stored in it by 64, rounding up, and you have the size of the array. Each element in the array consumes 8 bytes.
这意味着将其中存储的最大数字除以8,将大致得出BitSet的大小(以字节为单位). 大约"是由于虚拟机内存管理的开销,因为指向数组的指针也需要一些内存,并且因为数组本身也有一些开销.
That means that dividing by 8 the greatest number stored in it, roughly yields the size in bytes of the BitSet. "Roughly" because of virtual machine memory management overheads, because the pointer to the array also needs some memory and because the array itself has some overhead.
BitSet中存储的元素的插入顺序或实际数量对分配的内存大小没有影响.
The order of insertion or the actual number of elements stored in the BitSet have no influence on the size of the memory allocated.
对于您给出的两个示例,只需要一个Long-element即可使用8个字节的内存来存储数字,因为在两个示例中,最大数字均小于64.
For the two examples you give, only one Long-element is required to store the numbers, using 8 bytes of memory, as in both examples the highest number is less than 64.
一个存储任意五个数字的Ints数组将消耗5 * 4字节= 20字节加上开销.要存储n个数字,您大约需要n * 4个字节.
An array of Ints, storing any five numbers, would consume 5 * 4 bytes = 20 bytes plus overhead. For storing n numbers you need roughly n * 4 bytes.
因此,您正在将(highestNumberStored/8)个字节与(countOfNumbersStored * 4)个字节进行比较.
So you are comparing (highestNumberStored / 8) bytes against (countOfNumbersStored * 4) bytes.
这篇关于Scala中的BitSet内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!