我有一个程序,最终应该生成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选项和任何选项BEFORE VeryLargeObject,否则您将参数传递给java程序而不是JVM