java micro-optimization: combine set of boolean instance variables to bit vector based on int
Asked Answered
A

2

6

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?

Afford answered 2/6, 2015 at 8:14 Comment(1)
Please be more precise on high number of instances - do they all reside in memory at once or are you just making (and discarding) many, and how many are we talking about, thousands? Meeelions?? Please be more precise on many boolean instance variables - how many?Heraclea
C
5

A boolean uses 1 byte of memory (on hotspot). You could use alternatives:

  • a BitSet: uses approximately 1 bit per boolean + the overhead of the class itself, the reference to the BitSet, the reference to the long[] in the BitSet and the unused space in the long[], i.e. around 20 bytes
  • an int where each bit is a boolean to store 32 booleans in 4 bytes
  • a long where each bit is a boolean to store 64 booleans in 8 bytes

The JVM is unlikely to do that optimisation for you (hotspot 8 doesn't).

Cremona answered 2/6, 2015 at 8:22 Comment(2)
So, for saving memory by using BitSet requires at least 20x8=160 bits. That is a lot of bits for a single class. Well, then I compact them by handAfford
@UlrichScholz It depends on how many booleans you have - if you have less than 64 of them, then int and long are by far the best options - but if you have thousands of booleans, the overhead of a BitSet will be a much smaller percentage of the total memory used by the booleans.Cremona
D
5

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

Durrace answered 2/6, 2015 at 8:21 Comment(0)
C
5

A boolean uses 1 byte of memory (on hotspot). You could use alternatives:

  • a BitSet: uses approximately 1 bit per boolean + the overhead of the class itself, the reference to the BitSet, the reference to the long[] in the BitSet and the unused space in the long[], i.e. around 20 bytes
  • an int where each bit is a boolean to store 32 booleans in 4 bytes
  • a long where each bit is a boolean to store 64 booleans in 8 bytes

The JVM is unlikely to do that optimisation for you (hotspot 8 doesn't).

Cremona answered 2/6, 2015 at 8:22 Comment(2)
So, for saving memory by using BitSet requires at least 20x8=160 bits. That is a lot of bits for a single class. Well, then I compact them by handAfford
@UlrichScholz It depends on how many booleans you have - if you have less than 64 of them, then int and long are by far the best options - but if you have thousands of booleans, the overhead of a BitSet will be a much smaller percentage of the total memory used by the booleans.Cremona

© 2022 - 2024 — McMap. All rights reserved.