使用 -Xmx1G
标志提供一个一GB的堆,以下代码将按预期工作:
public class Biggy {
public static void main(String[] args) {
int[] array = new int[150 * 1000 * 1000];
}
}
该数组应该表示大约600 MB的数据。
然而,以下代码会抛出OutOfMemoryError错误:
public class Biggy {
public static void main(String[] args) {
int[] array = new int[200 * 1000 * 1000];
}
}
尽管该数组应该表示大约800 MB,因此可以轻松地放入内存中。
那么缺失的内存去哪了?