Standalone vs Plug-ins
You can install the Memory Analyzer into an Eclipse IDE (see [1]). However, a standalone Memory Analyzer is useful if you do not want to install a full-fledged IDE on the system you are running the heap analysis. To download a standalone Memory Analyzer, click here.
Prerequisite
For this illustration, we have downloaded
Eclipse Memory Analyzer Version 1.10.0 ―Windows (x86_64)
which requires a minimum Java version of 1.8.0.
Setups
After unzipping the file, a new folder named mat was created:
C:\Users\<username>\Downloads\mat
You can create a Windows Command Script (.cmd) with the same name in the same folder using the following content:
set PATH=C:\Program Files\Java\jdk-10\bin;
start MemoryAnalyzer.exe
To avoid running MAT with the wrong JRE, we have set its PATH environment variable pointing to a JDK installation with Java version of 18.3.0:
$ cd "/cygdrive/c/Program Files/Java/jdk-10/bin"
$ ./java.exe --version
java 10 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)
To enable MAT handling large heap dumps (i.e., .hprof), you may increase the heap size of MAT runtime by changing its MemoryAnalyzer.ini in the same folder:[7]
-startup
plugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.700.v20180518-1200
-vmargs
-Xmx10g
For example, we have set its max heap size to be 10 GB.
Getting a Heap Dump
The Memory Analyzer can work with HPROF binary formatted heap dumps. Those heap dumps are written by HotSpot and any VM derived from HotSpot. Depending on your scenario, your OS platform and your JDK version, you may have different options to acquire a heap dump.[2]
As a developer, you want to trigger a heap dump on demand. On Windows, use JDK and JConsole. On Linux and Mac OS X, you can also use jmap or jcmd that comes with JDK.
Via MAT:
- You can use Acquiring Heap Dump Dialog to acquire a heap dump from a locally running Java process
Via Java VM parameters:
- -XX:+HeapDumpOnOutOfMemoryError –XX:HeapDumpPath=[file path]
- writes heap dump on the first Out Of Memory Error (recommended)
- -XX:+HeapDumpOnCtrlBreak
- writes heap dump together with thread dump on CTRL+BREAK
- -XX:+HeapDumpOnCtrlBreak in HotSpot JVM (by Sun/Oracle) is present in 1.4.2_12 or higher and 1.5.0_14 or higher. For JVMs 1.6, 1.7, 1.8 this option is no more present, but you can use the "jmap" or "jcmd" tools.
Via Tools:
- jmap
- jmap -dump:format=b,file=snapshot.jmap <process id>
- It is recommended to use the latest utility jcmd instead of jmap utility for enhanced diagnostics and reduced performance overhead.
- jcmd
- jcmd <process id/main class> GC.heap_dump Myheapdump
- JConsole
- Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean
- SAP JVMMon
- Launch jvmmon.exe and call menu for dumping the heap
bash-4.2$ ${JAVA_HOME}/bin/jmap -dump:format=b,file=MyJmapHeapdump.hprof 82734
Dumping heap to /tmp/NM_OOM/MyJmapHeapdump.hprof ...
Heap dump file created
Example 2 Create a Heap Dump using jcmd
$ /bi/app/jdk/bin/jcmd 82734 GC.heap_dump MyJcmdHeapdump.hprof
82734:
Heap dump file created
Notes
- By default, the heap dump will be generated in the "current directory" of the Java application.[6]
- jmap generates heap dump in the directory where you run jmap, which is different from jcmd's behavior.
- The dump file can be huge, up to Gigabytes, so ensure that the target file system has enough space.
- Need to login as the same user as your process' to attach to it
References
- Standalone MAT downloads
- MemoryAnalyzer Getting Started
- Eclipse Memory Analyzer (MAT) - Tutorial
- Using HeapDumpOnOutOfMemoryError parameter for heap dump for JBoss
- Acquire Heap Dump from MAT (Memory Analyzer Tool)
- Diagnose Leaks in Java Language Code
- Problems Starting the Memory Analyzer
- Basic Concepts of Java Heap Dump Analysis with MAT
