简体   繁体   English

Scala中的BitSet内存使用情况

[英]BitSet memory usage in Scala

I would like to know what is the memory usage of BitSet in Scala.For example, if I do: 我想知道Scala中BitSet的内存使用情况。例如,如果我这样做:

  var bitArray:BitSet=new BitSet(10)
  bitArray.add(0)
  bitArray.add(2)
  bitArray.add(4)
  bitArray.add(6)
  bitArray.add(8)

How does that compare with and array containing the even numbers 0,2,4,6,8? 与包含偶数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)

How does that compare to the number 47? 这与47号相比如何?

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). 但作为一个更开放的问题,如果您知道,BitSet(WR对其他常见数据类型)的优点/缺点或用途是什么。

Thanks, 谢谢,

You can look at the implementation of BitSet in Scala 2.8 here: scala.collection.mutable.BitSet . 您可以在这里查看Scala 2.8中BitSet的实现: scala.collection.mutable.BitSet

It is implemented based on an array of Longs. 它基于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. 将存储在其中的最大数字除以64,向上舍入,并且您具有数组的大小。 Each element in the array consumes 8 bytes. 数组中的每个元素占用8个字节。

That means that dividing by 8 the greatest number stored in it, roughly yields the size in bytes of the BitSet. 这意味着除以8存储在其中的最大数字,大致产生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. “粗略”是因为虚拟机内存管理开销,因为指向数组的指针也需要一些内存,因为数组本身有一些开销。

The order of insertion or the actual number of elements stored in the BitSet have no influence on the size of the memory allocated. 插入顺序或存储在BitSet中的实际元素数量不会影响分配的内存大小。

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. 对于您给出的两个示例,使用8个字节的内存只需要一个Long元素来存储数字,因为在两个示例中,最高数字小于64。

An array of Ints, storing any five numbers, would consume 5 * 4 bytes = 20 bytes plus overhead. 存储任意五个数字的Ints数组将消耗5 * 4字节= 20字节加上开销。 For storing n numbers you need roughly n * 4 bytes. 要存储n个数字,您需要大约n * 4个字节。

So you are comparing (highestNumberStored / 8) bytes against (countOfNumbersStored * 4) bytes. 因此,您将(highestNumberStored / 8)字节与(countOfNumbersStored * 4)字节进行比较。

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

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