gpt4 book ai didi

java - OutOfMemory 时生成 java 转储

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:59:37 27 4
gpt4 key购买 nike

我有一个最终会生成 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.<init>(VeryLargeObject.java:14)
at VeryLargeObject.main(VeryLargeObject.java:32)

最佳答案

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

关于java - OutOfMemory 时生成 java 转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4935520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com