Java integer flag and bitwise operations for memory reduction

不打扰是莪最后的温柔 提交于 2019-12-03 03:49:11

It is my understanding that commonly a boolean is stored as an int in a JVM implementation. Is this correct?

It depends on the JVM implementation of course, but is probably true for implementations on mainstream CPUs.

In which case surely the 32 flags represent a large memory footprint reduction.

If you actually have 32 flags in a class, and a large number of instances of that class, yes. If you never have more than a few hundred instances, it's not worth worrying about.

It is my understanding that CPUs are very number driven and bitwise operations are about as efficient as things come in computing.

This is true.

Is there a performance penalty - or even gain - to using bitwise operations over boolean operations?

That depends also on the memory usage. If you work very intensively with only a few objects, the bitwise operations may slow things down. If you have lots of objects, the reduced memory will probably improve performance a lot due to better caching behaviour.

Is there a better way of accomplishing the same thing? Does an Enum allow the combination of flags i.e. FLAGX = FLAG1 | FLAG2?

Instead of doing the bitwise operations yourself, you can (and should) use a BitSet. And yes, it would be even cleaner if you could work with Enums and an EnumSet, but if you have a number of enums with a few elements each, it would probably not yield the desired memory savings, due to the overhead for multiple EnumSet instances.

Yes, a boolean is stored as a 32 bit int. Method signatures distinguish between booleans and ints but otherwise they are treated the same. This is part of the JVM byte code specification.

The bitwise operations on Java map directly to a single instruction on the CPU which does the same bitwise operation, so it is indeed pretty fast. Of course it is faster to keep each value in its own 32 bit word (then you do not need to do any bitwise operations at all). You will save memory and spend more CPU cycles using your approach.

Java has no operator for combining enum, and I do not see how that would make any sense anyway...

Is using an integer flag and bitwise operations an effective way of reducing the memory footprint of high volume Objects?

If an object has a large number of these flags AND there are huge numbers of these objects (or reducing heap usage is a critical issue) then this may be a worthwhile micro-optimization. Otherwise no (IMO).

The problem with this is that it makes your code harder to read and more fragile. So you really should only contemplate it if memory usage is a critical issue.

As you can't trust booleans are stored as bits or integers (I think the latter is the actual implementation), yes I think bit masks and flags do improve performance.

I have used them on some projects. They are less readable, but I never minded about that since my opinion is that every programmer should understand binary notation and arithmetic.

Just a recommendation: as for java 7, we can define the numeric literals like this:

   private static final int ENTER_TRAP = 0b00000000000100000000000000000000;

And even like this:

   private static final int ENTER_TRAP = 0b0000_0000_0001_0000_0000_0000_0000_0000;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!