How to get a heap dump of a remote machine via JMX?
When hunting memory leaks, you need a basis to work with. One of the most valuable things is a memory dump of the Java Virtual Machine. Those snapshots of the current state of the JVM are called heap dumps. They contain all the objects living in the JVM and a lot of other information needed to debug certain operations problems. So, how to get a heap dump of a remote machine? The following steps show you how to get a heap dump off of a remote JVM, e.g. running on your test server.
- Enable JMX by adding the following system properties to the java command line arguments. Note that disaling security is of course not recommended, but for a local development environment, it may be okay for you. For Tomcat, add it to CATALINA_OPTS in catalina.sh
- Connect to the remote machine using VisualVM via JMX: myserver:11391
- Go to the MBeans tab, open the com.sun.management folder and select the HotSpotDiagnostic MBean.
- In the Operations tab, enter the filename (e.g. “dump1.hprof“) into the input field p0. Keep the value “true” in the p1 field. Click on the dumpHeap button. The JVM will halt for a moment to write the file.
- Find the heap dump, it will be in the current working directory of the process. If you don’t know where the cwd of the process is, do a ps for the process to get the pid and use pwdx to find the current working directory:
Hint: Although .hprof files are binaries, they usually have a very good compression ratio, so try bzipping them before transferring them to save time and bandwidth (in this case, from 350MB to 50MB):