Reputation: 153
I have a list of Bitset having 25Million Bitset. Each Bitset I'm creating by using:
Bitset.valueOf(new long[] {1})
The memory being consumed is around 1300MB. Ie: in an average its taking 52bytes. I'm not understanding why so much of memory is being consumed.
Upvotes: 0
Views: 499
Reputation: 88707
Create a heapdump and check it with some analyzer (e.g. 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):
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).
25 million bits alone would need around 3 MB (e.g. if you'd create a BitSet that large).
As you can see, a BitSet containing only one bit is a huge waste of memory. 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. That would still need around 95MB though.
Upvotes: 1
Reputation: 201447
Each BitSet
is itself an object instance (each reference consumes memory). 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);
Upvotes: 2