本文介绍了在OutOfMemory时生成java转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program that should eventually generate OutOfMemory .The program code is:

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

For starter drop the XX options and any options BEFORE VeryLargeObject, otherwise you pass the parameters to the java program and not the JVM

这篇关于在OutOfMemory时生成java转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 17:51