简体   繁体   English

Java 32位系统BitSet的内存大小

[英]Memory size of Java 32-bit system BitSets

How to compute the memory of new BitSet(n) in C++. 如何在C ++中计算new BitSet(n)的内存

What memory takes the new BitSet(1024) in Java. Java new BitSet(1024)占用什么内存

But it seems the formula for Java is different. 但是似乎Java的公式不同。 I want to compute the memory spent for new BitSet(100000) , could you please help? 我想计算用于new BitSet(100000)的内存,请您帮忙吗?

BitSet are packed into arrays of "words." BitSet被打包到“单词”的数组中。 A word is (in the current implementation) a long. 一个单词(在当前实现中)很长。 The single bits inside will be retrieved / set uses masking; 里面的单个位将被检索/使用掩码设置; therefore it internally packs 64 bits in one single long value, and uses an array of longs to hold all the bits you need. 因此,它在内部将64位压缩为一个long型值,并使用long型数组来容纳所需的所有位。

The dimension of the array will be N (100000) / 64 bytes, or 1563 longs, or 12504 bytes, plus a fixed overhead needed by BitSet for its internal structure/bookeeping. 数组的尺寸为N(100000)/ 64字节,或者1563长,或者12504字节,再加上BitSet为其内部结构/布管所需要的固定开销。

See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/BitSet.java for the implementation; 有关实现,请参见http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/BitSet.java counting the fields and summing up the space they need (an int: 4 bytes; a long: 8 bytes, and so on) you can understand how much is the fixed overhead. 计算字段并汇总所需的空间(整数:4个字节;长整数:8个字节,依此类推),您可以了解固定开销是多少。

It is a little more than 100000/8 which basically the same as in C++ assuming N is the number of bits. 假设N是位数,它比100000/8多一点,与C ++基本相同。 To measure it exactly you can test it. 要精确测量它,您可以对其进行测试。

public static void main(String... ignored) {
    BitSet warmup = new BitSet(10000);    // load the class etc.

    long before = memoryUsed();
    BitSet bs = new BitSet(100000);
    long size = memoryUsed() - before;
    if (size == 0)
        throw new AssertionError("You need to run this with -XX:-UseTLAB for accurate accounting");
    System.out.printf("BitSet(100000) used %,d bytes%n", size);
}

public static long memoryUsed() {
    return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}

prints with -XX:-UseTLAB on the command line 在命令行上使用-XX:-UseTLAB打印

BitSet(100000) used 12,544 bytes

There is two objects created (the BitSet and the long[]) which accounts for the small difference from expected. 创建了两个对象(BitSet和long []),它们与预期的差异很小。

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

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