As the Java specification does not mandate memory use, this depends on the JVM implementation you use.
Certainly, there will be a per-object overhead in all JVM implementations (to implement run time type checking, for instance). A JVM might elect to memory align fields (on some platforms, this gives a significant speedup when accessing the field).
However, I'd be greatly surprised if array members where padded for memory alignment, and can confirm that (at least on the oracle vm for windows) a boolean[] takes one byte per element.
Also, it's worth noting that the size of fields of reference type can be 8 bytes if you happen to address a suitably large heap.
To conclude: If you really want to know, measure memory consumption on your target JVM.
Edit: Out of curiosity, I wrote a small (inaccurate) benchmark:
class FourBytes {
byte a,b,c,d;
}
public class Test {
long usedBefore = used();
long used() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
public void before() {
System.gc();
usedBefore = used();
}
public void after(String text) {
long usedAfter = used();
System.out.println(text + "\t" + new BigDecimal(usedAfter - usedBefore).movePointLeft(6) + " bytes");
before();
}
{
int max = 1000000;
before();
boolean[] bools = new boolean[max];
after("boolean in array");
char[] chars = new char[max];
after("char in array ");
Object[] objects = new Object[max];
after("reference type in array");
for (int i = 0; i < max; i++) {
objects[i] = new Object();
}
after("Object instance ");
Byte[] bytes = new Byte[max];
before();
for (int i = 0; i < max; i++) {
bytes[i] = new Byte((byte) i);
}
after("Byte instance ");
Integer[] integers = new Integer[max];
before();
for (int i = 0; i < max; i++) {
integers[i] = new Integer(i);
}
after("Integer instance");
FourBytes[] fbs = new FourBytes[max];
before();
for (int i = 0; i < max; i++) {
fbs[i] = new FourBytes();
}
after("FourBytes instance");
}
public static void main(String[] args) throws Exception {
new Test();
}
}
On
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) Client VM (build 22.0-b10, mixed mode, sharing)
it prints:
boolean in array 1.183624 bytes
char in array 2.091768 bytes
reference type in array 4.091768 bytes
Object instance 8.023664 bytes
Byte instance 16.133408 bytes
Integer instance 16.147312 bytes
FourBytes instance 16.142568 bytes