We have a class with many instances and run into memory problems. Therefore, we try to reduce the memory requirements of this class. One idea would be the following.
The class has many boolean instance variables, each of which would take up one word in a naïve implementation. One could think of combining them to a mini bit vector stored in an int, such that their combined memory requirement would be one word.
But I would suspect that the Java VM is doing this optimization anyways, such that performing it by hand would not get any additional savings. Right?
A boolean uses 1 byte of memory (on hotspot). You could use alternatives:
long[]
in the BitSet and the unused space in the long[]
, i.e. around 20 bytesThe JVM is unlikely to do that optimisation for you (hotspot 8 doesn't).
The JVM will not do that for you. The actual size in memory being used per boolean is usually around a byte, but in general, it's JVM dependent.
If you have that many boolean variables, you should think about using a BitSet, which is designed to use bits for representing boolean values.
See the Javadoc for reference:
http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With