2011-02-08 61 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