Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is byte more efficient than boolean[8] [duplicate]

Tags:

java

If I need 8 boolean member variables in a class, does Java effectively place them all in one byte? Or will it use a byte for each? In other words, is the memory footprint different for:

boolean a;
boolean b;
boolean c;
boolean d;
boolean e;
boolean f;
boolean g;
boolean h;

vs.

public static final int a = 0x01;
public static final int b = 0x02;
public static final int c = 0x04;
public static final int d = 0x08;
public static final int e = 0x10;
public static final int f = 0x20;
public static final int g = 0x40;
public static final int h = 0x80;
byte flags;

I'm asking because I will be instantiating a lot of these objects. So having it take 1 byte instead of 8 bytes of memory will be a noticeable savings.

Update: This is definitely similar to the linked questions that list that a boolean is stored in an int (thank you for those links and sorry I didn't find them before asking). This question is a little different in that it presents the specific alternative of using a byte and bit flags. I don't know if this is sufficient to make this question not a duplicate.

Update 2: I just ran this using SizeofUtil and found the following. The 8 booleans requires 24 bytes/object or 3 bytes/boolean. The single byte approach requires 10 bytes/object. I would understand 8 where it's expanding a byte to a native int (I'm on a 64-bit system). But what's with the other 2 bytes?

like image 467
David Thielen Avatar asked Dec 06 '13 17:12

David Thielen


People also ask

What is the difference between byte and boolean?

byte will return a value; bool returns TRUE/FALSE, when it contains either 0 or any value >0.

Is BitSet memory efficient?

In addition to throughput, we saw that the BitSet uses much less memory compared to a boolean[] with the same size. To recap, in single-bit read-heavy scenarios, the boolean[] outperforms the BitSet in smaller sizes. However, when the number of bits increases, the BitSet has superior throughput.

Why is a Boolean 4 bytes?

Because it's fast. A 32-bit processor typically works with 32-bit values. Working with smaller values involves longer instructions, or extra logic.

Why boolean is one byte?

A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed. you can't store a variable of size less than 1 byte.


2 Answers

The actual information represented by a boolean value in Java is one bit: 1 for true, 0 for false. However, the actual size of a boolean variable in memory is not precisely defined by the Java specification.

The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

Hence it will be better to use byte if you are creating lots of booleans.

like image 80
Amit Sharma Avatar answered Sep 26 '22 00:09

Amit Sharma


Read the topic "How much memory does a boolean consume?". They suggest BitSet as solution for large sets of booleans, but in your case byte solves the problem better, because you won't have a large set of booleans, you will have a large set of 8 booleans objects.

Summarizing: byte is better than 8 booleans.

like image 41
Italo Borssatto Avatar answered Sep 27 '22 00:09

Italo Borssatto