In Java, memory used for occupying the int[]
array of size n
equals to (4 + n) * 4
bytes.
Practically can be proven by the code below:
public class test {
public static void main(String[] args) {
long size = memoryUsed();
int[] array = new int[2000];
size = memoryUsed() - size;
if (size == 0)
throw new AssertionError("You need to run this with -XX:-UseTLAB for accurate accounting");
System.out.printf("int[2000] used %,d bytes%n", size);
}
public static long memoryUsed() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
}
so interesting is number 4
in parentheses. First portion of 4
bytes takes array reference, second - array length, then what takes 8 bytes left?