2011-02-08 62 views
5

我有一個應該最終生成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) 
+3

你的意思是說堆文件沒有生成?並且不應該是-XX:-HeapDumpOnOutOfMemoryError而不是-XX:+ HeapDumpOnOutOfMemoryError(注意+號) – CoolBeans 2011-02-08 16:29:40

+0

>程序失敗,出現OutOfMemory,但現在在生成文件時轉儲​​。 - 這是一個錯字嗎? - 你的意思是**現在**還是**沒有**? – Ralph 2011-02-08 16:43:13

回答

8

對於首發降XX選項和任何選項VeryLargeObject,否則將參數傳遞給java程序而不是JVM

5

我懷疑jvm無法寫入路徑並失敗。例如,這必須是存在的目錄中的文件名。如果你有一個目錄D:\workspace它會失敗。如果你有D:\workspace\heap.hprof它可能工作。嘗試首先創建一個名稱空白的文件,看看你能做到這一點。

+1

+1比我快25秒sec – Ralph 2011-02-08 16:51:51

+1

參數錯位 – bestsss 2011-02-08 17:27:25

14

問題是,-XX:HeapDumpPath描述文件而不是路徑。

-Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof" 

補充說:

bestsss是正確的,所以你需要解決兩個 「錯誤」:

java -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof" VeryLargeObject