I want to store System.currentTimeInMillis
in memory with minimum space possible. because I have to store millions of them in memory.
I converted it to binaryString
which gave me 41 bits
Here is my program
public class BitSetSize {
public static void main(final String[] args) {
final long currentTimeMillis = System.currentTimeMillis();
final String currentTimeToBinaryString = Long.toBinaryString(currentTimeMillis);
System.out.println("Size in bits: " + currentTimeToBinaryString.length());
final BitSet bitSet = BitSet.valueOf(new long[]{currentTimeMillis});
System.out.println("Bitset length: " + bitSet.length());
System.out.println("Bitset size: " + bitSet.size());
System.out.println("Size of biset object(bytes): " + MemoryMeasurer.measureBytes(bitSet));
}
}
But when I run it I get
Size in bits: 41
Bitset length: 41
Bitset size: 64
Size of biset object(bytes): 48
Question
- Why does bitSet.length()
and bitSet.size()
differ? I assume length()
is correct?
- I am using memory-measurer to learn about the size of bitSet
, but it tell me 48 bytes
, why is it not (41/8) byte
?
I am confused