我有一個應該最終生成OutOfMemory
的程序。 程序代碼是:當OutOfMemory生成Java轉儲
public class VeryLargeObject implements Serializable {
public static final int SIZE = 1 << 12;
public String tag;
public int[][] bigOne = new int[SIZE][SIZE];
{
// Initialize bigOne
for(int i = 0; i < SIZE ; ++i) {
for(int j = 0; j < SIZE; ++j) {
bigOne[i][j] = (int) (Math.random() * 100);
}
}
}
public VeryLargeObject(String tag) {
this.tag = tag;
}
public static void main(String args[]) {
VeryLargeObject[] vla = new VeryLargeObject[1 << 12];
for(int i = 0; i < Integer.MAX_VALUE; ++i) {
vla[i] = new VeryLargeObject("aa");
}
}
}
我用下面的參數運行程序:
java VeryLargeObject -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="D:\workspace"
該計劃失敗,內存溢出,但不會生成轉儲文件。你有什麼想法,爲什麼?
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at VeryLargeObject.<init>(VeryLargeObject.java:14)
at VeryLargeObject.main(VeryLargeObject.java:32)
你的意思是說堆文件沒有生成?並且不應該是-XX:-HeapDumpOnOutOfMemoryError而不是-XX:+ HeapDumpOnOutOfMemoryError(注意+號) – CoolBeans 2011-02-08 16:29:40
>程序失敗,出現OutOfMemory,但現在在生成文件時轉儲。 - 這是一個錯字嗎? - 你的意思是**現在**還是**沒有**? – Ralph 2011-02-08 16:43:13