5

My searches on SO have failed me, so if this is a duplicate, please redirect me.

With that out of the way, my question: I learned, from experience and browsing SO that a Java boolean is stored as a 32-bit int if you declare it as a standalone value, but as an 8-bit byte if you declare it within an array. My question, here, is as follows: Which is more memory efficient? Does the meta data of the array make it bigger in memory than the alternative?

boolean oneVariable = false, oneArray[] = {false};
2
  • Isn't that comparing apples and pears? The first has value semantics, and the second has reference semantics. Pick the one you need. Commented Jan 22, 2012 at 6:03
  • 3
    It's comparing memory management techniques, so like tangerines versus clementines. Commented Jan 22, 2012 at 6:09

4 Answers 4

7

The Array is an actual Object that comes with a memory penalty (I believe 12 bytes) So the primitive boolean is smaller.

Sign up to request clarification or add additional context in comments.

4 Comments

All Objects have 4 bytes for wait/notify, and 4 bytes for something else (not sure what). An array also has 4 bytes for the length. So 12. As Daniel noted, this size is rounded up to a multiple of 8. So the "12 bytes" I mentioned is really 16.
It has a reference to the class data structure this is 32-bit or 64-bit depending on the JVM. (You have to know what type the object is) An array has the "field" length which is 4-bit long. and there is 4-bytes for other things which include the object monitor.
Also all objects are created on an 8 byte boundary. i.e. they always reserve a multiple of 8 bytes. If that sounds in-efficient its worth nothing that 64-bit C & C++ have an 16 byte alignment. ;)
@Peter Lawrey I was wondering if some of the space was for the class info. Thanks for the clarification!
4

The "meta data" of the array includes:

  • 8 bytes (32-bit JVM) or 16 bytes (64-bit JVM) for object header
  • 4 bytes (32 bits) for the length of the array

Add on the 1 necessary byte for the boolean data and you have 13 bytes (32 bit) or 21 bytes (64 bit) at a minimum.

However, objects are allocated memory in 8-byte multiples, so even though you only need 12 or 20 bytes of overhead + 1 byte for the boolean, you'll end up using 16 or 24 bytes of memory, respectively, for your array object.

In addition to the 16/24 bytes the object itself will take up, you'll need 4 bytes (32 bit) or 8 bytes (64 bit) for the memory address of the object, totaling 20 or 32 bytes of memory, respectively, to store your boolean in an array.

The size of a standalone variable is JVM dependent. Java does not specify the size of storage, and in fact Oracle says

This data type represents one bit of information, but its "size" isn't something that's precisely defined.

Older JVMs use a 32-bit stack cell, used to hold local variables, method arguments, and expression values so a single boolean used as a variable would consume 4 bytes; making the array at least 5 times as expensive as for a single boolean. This answer may be different if, for example, the boolean is a class variable in which case it would just be a single byte added to the existing overhead. In newer JVMs a single boolean would only use 1 byte, but depending on its context and the 8-byte padding necessary to align memory addresses, could still consume up to 8 bytes of heap space. It would still be smaller than the boolean array.

1 Comment

The 4 extra bytes are for the length.
1

As user949300 mentioned, all objects carry a penalty that make them larger than primitives. For only a single boolean though, memory doesn't really matter. If you are storing a large number of booleans, consider using a BitSet. I believe under the hood it uses approximately 1 bit per boolean (plus some overhead).

1 Comment

For this thought experiment, it's a single boolean.
1

This Java specialist article is a good source for understanding the memory usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.