java OutOfMemory时生成java转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4935520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate java dump when OutOfMemory
提问by Andrei Ciobanu
I have a program that should eventually generate OutOfMemory
.
The program code is:
我有一个程序应该最终生成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");
}
}
}
I run the program with the following parameters:
我使用以下参数运行程序:
java VeryLargeObject -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="D:\workspace"
The program fails with OutOfMemory but no dump file is generated . Do you have any idea why?
程序因 OutOfMemory 而失败,但没有生成转储文件。你知道为什么吗?
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at VeryLargeObject.<init>(VeryLargeObject.java:14)
at VeryLargeObject.main(VeryLargeObject.java:32)
回答by Ralph
The problem is that -XX:HeapDumpPath
spefies a file and not a path.
问题是-XX:HeapDumpPath
指定文件而不是路径。
-Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof"
added:
添加:
and bestsssis right too, so you need to fix both "errors":
和bestsss是正确的,所以你需要解决两个“错误”:
java -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof" VeryLargeObject
回答by bestsss
For starter drop the XX options and any options BEFOREVeryLargeObject
, otherwise you pass the parameters to the java program and not the JVM
首先删除 XX 选项和任何选项BEFOREVeryLargeObject
,否则您将参数传递给 java 程序而不是 JVM
回答by Peter Lawrey
I suspect the jvm could not write to the path and fails silently. For example, this has to be a file name in a directory which exists. If you have a directory D:\workspace
it will fail. If you have D:\workspace\heap.hprof
it may work. Try creating a blank file of that name first to see you can do this.
我怀疑 jvm 无法写入路径并无提示地失败。例如,这必须是存在的目录中的文件名。如果您有目录D:\workspace
,它将失败。如果你有D:\workspace\heap.hprof
它可能工作。首先尝试创建一个具有该名称的空白文件,看看您是否可以做到这一点。