7

As i know the boolean size in 16 bytes {8 as header,1 payload ,* alignment to 8}

how much does it take if the boolean variable was an array ...

my reference

Hilmi
  • 3,411
  • 6
  • 27
  • 55
  • 1
    Why don't you just [measure it](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object) – jmj Jun 26 '12 at 07:04
  • 2
    Though it's not the answer to the question, but way better is to use `java.util.BitSet` which is designed specifically for storing array of boolean values. – Yuriy Nakonechnyy Jun 26 '12 at 09:00

2 Answers2

6

Are you asking about Boolean object or boolean primitive? The size of the object might be 16 bytes (although probably implementation dependent) while boolean will probably consume 4 bytes (int is implicitly used).

Thus boolean[] will consume N * 4 bytes (where N is the size of the array) + some object header. Boolean[] will consume N * 16 + header (according to your assumption on Boolean size.

That being said consider writing your own array-like class and pack 32 booleans in one int (you'll have to write few bit operations manually), just like BitSet class is doing.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
-1

The array Object size will be: 8 + 4 = 12 bytes (Here 4 is the length of array) If array length is N then the boolean elements will be: N*16 bytes So the size will be: (12 + N * 16) bytes rounded (ceil) by 8

As an example: if N =10, then 12 + 10 * 16 = 172 and after rounding the figure by JVM, the size will be 176 bytes.

rshahriar
  • 2,440
  • 1
  • 15
  • 5
  • 1
    The calculation above is incorrect. Firstly, the Boolean array will contain references to Boolean objects, there will be N references and their size is independent on the size of a Boolean object (references are typically 4 bytes in HotSpot). Also, the above seems to assume that all the Boolean objects are distinct. Typically you would reuse Boolean.TRUE and Boolean. FALSE, so there would be no extra space for the Boolean objects at all. – Per Mildner Oct 16 '14 at 11:51