Thursday 16 August 2018

Java: Generate heap dump on OutOfMemoryError (on application exit)

Start the application using '-XX:+HeapDumpOnOutOfMemoryError' option.

HeapEx.java
class HeapEx {
 public static void main(String args[]) {
  int size = 2147483647;
  
  HeapEx obj[] = new HeapEx[size];

  for (int i = 0; i < size; i++)
   obj[i] = new HeapEx();

  System.out.println("I am done");
 }
}


Compile HeapEx.java.

Run HeapEx application using below command.

java -classpath  . -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=overflowDump.hprof HeapEx

C:\Users\krishna\Documents\Study\Miscellaneous>java -classpath  . -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=overflowDump.hprof HeapEx
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
Dumping heap to overflowDump.hprof ...
Unable to create overflowDump.hprof: File exists
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
        at HeapEx.main(HeapEx.java:5)



Previous                                                 Next                                                 Home

No comments:

Post a Comment