在OutOfMemory时生成java转储

我有一个程序,最终应该生成OutOfMemory 。 程序代码是:

 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" 

程序因OutOfMemory失败,但未生成转储文件。 你知道为什么吗?

 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at VeryLargeObject.(VeryLargeObject.java:14) at VeryLargeObject.main(VeryLargeObject.java:32) 

对于启动器删除XX选项和任何选项BEFORE VeryLargeObject ,否则您将参数传递给java程序而不是JVM

问题是-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 

我怀疑jvm无法写入路径并无声地失败。 例如,这必须是存在的目录中的文件名。 如果您有目录D:\workspace ,它将失败。 如果你有D:\workspace\heap.hprof它可能会工作。 首先尝试创建该名称的空白文件,以确保您可以执行此操作。