Isaiah Taylor
Isaiah Taylor

Reputation: 627

Why do bools take less memory in an array?

On the Oracle website, it says that bools take 32 bits on the stack, but 8 in an array. I'm having trouble understanding why it is that they would take less in a group than in as singles. How are they stored, and what difference does it make? If arrays of bools are more efficient, why has that technology not been transferred over to singles?

Also, why not 1 bit?

And what is the difference between how a 64 but system and a 32 bit system stores these?

Thanks!

Upvotes: 2

Views: 850

Answers (3)

Ingo
Ingo

Reputation: 36339

Look, when it comes to stack, one must keep in mind that speed is the most important thing. For example, consider the following:

 void method(int foo, boolean bar, String name) ....

Then the stack just after entering the method looks like this:

 |-other variables-|-...-|-name-|-bar-|-foo-|---- return address etc. --
 ^
 stack pointer

These are all quantities on a word boundary, symbolized by |. Sure, the JVM could (theoretically, but see below) store the boolean in a single byte. But one must keep in mind that 32bit loads may be slower when they don't address word boundaries. Depending on architecture, it may be impossible to go through a pointer that does not live on a word boundary. Or it may be impossible use the quantity in a floating point instruction, etc. etc.

In addition, the byte code format can only address the n-th word on the stack. If this were not so, addresses relative to the stack pointer would have to be specified in bytes and this would mean that almost any stack access would have two bits that are irrelevant most of the time, as the majority of arguments will be words (int, float or reference) or double words (long, double).

What is never possible is to use 1 single bit for booleans. Why? Because bits are not directly addressable. The smallest addressable unit is the byte.

You can still store 32 booleans in an int if you feel that you should save on memory.

Upvotes: 3

Red Alert
Red Alert

Reputation: 3816

Because of the way CPUs work, all operations are done in 32 bits. If you have a single bool, the only realistic thing a compiler can do is zero out the rest of the 24 bits and save that to the stack, since it's not practical to scan your java file for other bools to and store them all in the same 32 bit memory block.

If you have an array of bools, it's simple to just reference them in blocks of 4, so it's only 8 bits per bool.

Note that this only applies to 32 bit applications/machines.

Upvotes: 3

Void Star
Void Star

Reputation: 2521

A boolean value can be stored as a single binary digit, but our computers group values as a convenience. The smallest unit practically dealt with is a byte, the next largest being a word. A byte is, in modern hardware, always 8 bits. 32 bits has emerged as the standard for a word. Even our 64 bit computers can deal in 32 bit words effectively. It is much more convenient to store a bool in whatever unit comes naturally than as a single bit. In an array, the natural unit would be a byte, since you can address any byte in memory. On the stack, which is a word stack, the natural unit is a word. You could stuff bools into bytes and words and work on pulling them out again bit by bit, literally, but that's less efficient than storing them in bytes or words because modern memories are large, so CPU speed is more of a concern. You wouldn't want to waste all of the time it takes to pack bits in compactly, so we waste memory instead, since it is more expendable.

Upvotes: 7

Related Questions