About the Memory debug and heap dump tips in Java

About the Memory debug and heap dump tips:
Feb 2016 By W.ZH

It is needed to debug the java memeory usage when you face some issues like the OutOfMemoryError in Java.

Here is summary some common usages I used in projects:

1. How to trigger a automatically memory dump for a JVM when it faces a OutOfMemoryError?

Just add these options to the Java

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=PathOfDumpFileFolder

It will create a java_pid.hprof file in the DumpPath.

2. If you want to make a dump anytime when your system is running, just run this command when your applications running:

jmap -dump:file=path_to_file 12341234 here is your java process id.

3. How to open the HPROF file ro read the content of the heap?

You can use the eclipse plugin to do this. Normally I use “Memory Analyzer”

After install the Plugin open this perspective to use it.

Window –> open perspective –> other –> Memmory Analysis

then go to menu of :

File –> Open Heap Dump  to read in a dump file.

4. How to dump in the eclipse environment?

Same with 3, use the “Memory Analyzer” http://www.eclipse.org/mat/

Use it to create a new dump project and do the dump. refer the some tips and tutorial of “Memory Analyzer”, Not hard to use it.