Generate java dump when OutOfMemory

I have a program that should ultimately generate OutOfMemory. Program code:

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 does not work with OutOfMemory, but a dump file is not created. Do you know, why?

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at VeryLargeObject.<init>(VeryLargeObject.java:14)
        at VeryLargeObject.main(VeryLargeObject.java:32)
+5
source share
3 answers

To start the starter, parameters XX and any options BEFORE VeryLargeObject, otherwise you will pass the parameters to the java program, not the JVM

+8
source

The problem is what the -XX:HeapDumpPathfile specifies, not the path.

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

added by:

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

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

I suspect jvm could not write to the path and fail silently. For example, this should be the name of a file in a directory that exists. If you have a directory D:\workspace, it will not work. If you have D:\workspace\heap.hprof, this might work. First, try creating an empty file with that name to see that you can do it.

+5
source

All Articles