In Java, la memoria utilizzata per l'occupazione dell'array int[]
della dimensione n
corrisponde a (4 + n) * 4
byte.Dimensioni di memoria di un sistema Java a 32 bit int [] array
Praticamente può essere dimostrata con il codice qui sotto
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();
}
}
così interessante è il numero 4
tra parentesi. La prima porzione di 4
byte prende il riferimento dell'array, la seconda lunghezza dell'array, quindi cosa rimane da 8 byte?