How to generate Heap dump when OutOfMemoryError is thrown in java?



Contents of page >
  • 1) What is purpose of using generating heap dump when OutOfMemoryError is thrown in java?
  • 2) How to generate Heap dump when OutOfMemoryError is thrown in java?
  • 3) What will be the location (Directory) of heap dump generated?
  • 4) How to change location (Directory) of heap dump generated?
  • 5) So, let’s see how to pass HeapDumpOnOutOfMemoryError and HeapDumpPath VM parameters together in cmd and eclipse >
    • 5.1) So, let’s see how to use both VM parameters together in cmd>
    • 5.2) Passing HeapDumpOnOutOfMemoryError and HeapDumpPath both of them in eclipse>
  • 6) Analyze generated heap dump using jhat >


1) What is purpose of using generating heap dump when OutOfMemoryError is thrown in java?
It is the perfect time to get your heap details (i.e. heap dump) and to spot what exactly went wrong at the time when OutOfMemoryError is thrown in java, we can use it for analyzing the garbage collection in java.


2) How to generate Heap dump when OutOfMemoryError is thrown in java?
Heap dump will be generated when OutOfMemoryError is thrown by specifying -XX:+HeapDumpOnOutOfMemoryError VM option.


3) What will be the location (Directory) of heap dump generated?
The heap dump will be generated in the Java working directory.

4) How to change location (Directory) of heap dump generated?
We can specify -XX:HeapDumpPath=E:\file_name.dump vm option, then heat dump will be generated in specified path.

5) So, let’s see how to pass HeapDumpOnOutOfMemoryError and HeapDumpPath VM parameters together in cmd and eclipse >

5.1) So, let’s see how to use both VM parameters together in cmd>
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\heapDumpOnOutOfMemory.dump MyJavaProgram


5.2) Passing HeapDumpOnOutOfMemoryError and HeapDumpPath both of them in eclipse>

For more details read : How to pass vmArgs(JVM parameters) to java program in eclipse



6) Analyze generated heap dump using jhat >

Use jhat E:\heapDumpOnOutOfMemory.dump

C:\>jhat E:\heapDumpOnOutOfMemory.dump
Reading from E:\heapDumpOnOutOfMemory.dump...
Dump file created Mon Nov 07 23:59:19 IST 2016
Snapshot read, resolving...
Resolving 241865 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

By default, it will start http server on port 7000.
Then we will go to http://localhost:7000/

Where we can find all the objects which were created in java program.







Summary -

So in this core java tutorial we learned how to use JHAT to analyze the heat dump - Using jmap as well in java.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java


What are -Xms and -Xmx JVM parameters in java, And differences between them with examples


How to use -verbose:gc VM argument

How to use Jstat for monitoring the garbage collection in java

What is vmid - and how to find it using JPS


eEdit
Must read for you :