8

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)
4
  • 3
    Did you mean to say the heap file didnt generate? And shouldn't it be -XX:-HeapDumpOnOutOfMemoryError instead of -XX:+HeapDumpOnOutOfMemoryError (notice the + sign)
    – CoolBeans
    Feb 8, 2011 at 16:29
  • >The program fails with OutOfMemory but now dump if file is generated. -- Is it a typo? - do you mean now or no?
    – Ralph
    Feb 8, 2011 at 16:43
  • @CoolBeans: The official site: docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/… says that the option shoud be with + sign Jun 8, 2018 at 8:41
  • @BalabanMario Hi - I am sorry, I am not sure if I follow. So the official docs say the same thing right? "-XX:+HeapDumpOnOutOfMemoryError" ? Were you just confirm that?
    – CoolBeans
    Jun 8, 2018 at 15:55

3 Answers 3

19

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
1
  • 3
    '-XX:HeapDumpPath' can also be a path, like this: '-XX:HeapDumpPath=c:/', which will generate dumps with the pattern "java_pid4128.hprof" in the root direcoty of drive C:
    – It's Leto
    Mar 10, 2014 at 17:45
9

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

3
  • i dont find any documentation for VeryLargeObject , can u give us some authentic link.
    – Vipin
    Feb 4, 2014 at 7:01
  • @Vipin, 'VeryLargeObject' is the class containing the main(String[]). Look at the question, itself.
    – bestsss
    May 5, 2014 at 14:24
  • i did not notice it , well now i got it :)
    – Vipin
    May 7, 2014 at 8:49
5

I suspect the jvm could not write to the path and fails silently. For example, this has to be a file name in a directory which exists. If you have a directory D:\workspace it will fail. If you have D:\workspace\heap.hprof it may work. Try creating a blank file of that name first to see you can do this.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.