10
Java에서 int[]
배열의 크기가 n
인 메모리는 (4 + n) * 4
바이트와 같습니다.Java 32 비트 시스템의 메모리 크기 int [] array
는 실질적으로 아래의 코드에 의해 입증 될 수있다 :
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();
}
}
매우 흥미로운 괄호 안의 숫자 4
입니다. 4
바이트의 첫 번째 부분은 배열 참조, 두 번째 배열 길이를 취하고 8 바이트가 남았습니까?