The Eclipse Memory Analyzer is a fast and feature-rich heap analyzer that helps you find memory leaks and high memory consumption issues.
Download the latest version, run the Memory Analyzer Basics Cheat Sheet (Help -> Cheat Sheets...).
A good starting point might also be this blog about Finding a Leaking Workbench Window.
Getting a Heap Dump
The Memory Analyzer works with heap dumps, more specifically HPROF binary formatted heap dumps. Those heap dumps are written by Sun HotSpot and any VM derived from HotSpot. Depending on your scenario, your OS platform and your JDK version, you have different options to acquire a heap dump.
If you run your application with the VM flag -XX:+HeapDumpOnOutOfMemoryError a heap dump is written on the first Out Of Memory Error. There is no overhead involved unless a OOM actually occurs. This flag is a must for production systems as it is often the only way to further analyze the problem.
As a developer, you want to trigger a heap dump on demand. On Windows, use JDK 6 and JConsole. On Linux and Mac OS X, you can also use jmap that comes with JDK 5.
Via Java VM parameters
- -XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)
- -XX:+HeapDumpOnCtrlBreak writes heap dump together with thread dump on CTRL+BREAK
- -agentlib:hprof=heap=dump,format=b combines the above two settings (old way; not recommended as the VM frequently dies after CTRL+BREAK with strange errors)
- Sun JMap: jmap.exe -dump:format=b,file=HeapDump.hprof <pid>
- Sun JConsole: Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean
- SAP JVMMon: Launch jvmmon.exe and call menu for dumping the heap
Heap dump will be written to the working directory.
|Vendor / Release||VM Parameter||VM Tools|
|On OoM||On Ctrl+Break||Agent||JMap||JConsole|
|1.5.0_07||Yes||Yes||Yes (Only Solaris and Linux)|
|1.5.0_07||Yes||Yes||Yes||Yes (Only Solaris and Linux)|
Do you Support IBM System Dumps?
Sorry, currently only HPROF formatted heap dumps are supported. However, we are working together with IBM to support IBM system dumps based on DTFJ. We expect a solution around Q4 2008.
What if the Heap Dump is NOT Written on OutOfMemoryError?
Heap dumps are not written on OutOfMemoryError for the following reasons:
- Application creates and throws OutOfMemoryError on its own
- Another resource like threads per process is exhausted
- C heap is exhausted
As for the C heap, the best way to see that you won't get a heap dump is if it happens in C code (eArray.cpp in the example below):
# An unexpected error has been detected by SAP Java Virtual Machine: # java.lang.OutOfMemoryError: requested 2048000 bytes for eArray.cpp:80: GrET*. Out of swap space or heap resource limit exceeded (check with limits or ulimit)? # Internal Error (\\...\hotspot\src\share\vm\memory\allocation.inline.hpp, 26), pid=6000, tid=468
C heap problems may arise for different reasons, e.g. out of swap space situations, process limits exhaustion or just address space limitations, e.g. heavy fragmentation or just the depletion of it on machines with limited address space like 32 bit machines. The hs_err-file will help you with more information on this type of error. Java heap dumps wouldn't be of any help, anyways.
Also please note that a heap dump is written only on the first OutOfMemoryError. If the application chooses to catch it and continues to run, the next OutOfMemoryError will never cause a heap dump to be written!
Finding Memory Leaks
The Memory Analyzer grew up at SAP. Back then, Krum blogged about Finding Memory Leaks with SAP Memory Analyzer. The content is still relevant!
Analyzing Java Collection Usage
Check out Krum's blog about Analyzing Java Collections Usage with Memory Analyzer. Also, Memory for Nothing looks unused collections and the memory kept alive.
Perm Space Issus
Vedran has blogged some hints how to address Perm Space Issues.