Generate java dump when OutOfMemory

Andrei Ciobanu picture Andrei Ciobanu · Feb 8, 2011 · Viewed 20.5k times · Source

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");
        }
    }
}

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?

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

Answer

Ralph picture Ralph · Feb 8, 2011

The problem is that -XX:HeapDumpPath spefies a file and not a path.

-Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof"

added:

and bestsss is right too, so you need to fix both "errors":

java -Xms1024m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="c:\temp\dump2.hprof" VeryLargeObject