3

Is it possible to get a server heap dump on a running process on linux (CentOS) using JMX from command line ?

can't open VisualVM, can't install jmap

trincot
  • 317,000
  • 35
  • 244
  • 286
Nati
  • 1,034
  • 5
  • 19
  • 46

2 Answers2

6

It can be done with this simple code:

import com.sun.management.HotSpotDiagnosticMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

@SuppressWarnings("restriction")
public class CreateHeapDump
{
    public static void main(String[] args) throws Exception
    {
        String host = args[0];
        String port = args[1];

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        ObjectName mbeanName = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
        HotSpotDiagnosticMXBean bean = JMX.newMBeanProxy(mbsc, mbeanName, HotSpotDiagnosticMXBean.class, true);

        String fileName = "heap_dump_" + new SimpleDateFormat("dd.MM.yyyy HH.mm").format(new Date()) + ".hprof";
        boolean onlyLiveObjects = true;
        bean.dumpHeap(fileName, onlyLiveObjects);
    }
}

Compile it:

javac CreateHeapDump.java

Call it from command line:

java CreateHeapDump localhost 9010
user988346
  • 1,799
  • 3
  • 16
  • 15
Vadim Zverev
  • 468
  • 6
  • 14
0

This won't be pretty, but it works. Having said that, you might want to consider scripting this in Groovy or Jython, or even JavaScript.... I added a quickie add-on to jmxlocal, a project which implements standard JMX remoting for local JVMs. It now supports a command line invocation of one command against the connected MBeanServer, and the command must be specified in Java code.

Clone the repo and build with mvn clean install. Copy the jar (jmxlocal-1.0-SNAPSHOT.jar) to your target server. Execute the dump JMX command using the PID of the target java process as follows:

java -jar target/jmxlocal-1.0-SNAPSHOT.jar -j service:jmx:attach:///<PID> -c "conn.invoke(on(\"com.sun.management:type=HotSpotDiagnostic\"), \"dumpHeap\", new Object[]{\"/tmp/heap.dump\", true}, new String[]{String.class.getName(), boolean.class.getName()})"

The output will be

Command Executed. Result [null]

and you should find your dump file at /tmp/heap.dump.

If you need to, you can supply credentials using the -u [username] and -p [password] arguments.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • `java.lang.RuntimeException: Failed to find the Atach API. Please add tools.jar to the classpath`. any ideas ? – Nati Mar 01 '16 at 21:20
  • This usually happens if you're using the JRE (instead of the JDK) which does not have **tools.jar**. That jar can be found in **$JDK_HOME**/lib. – Nicholas Mar 01 '16 at 21:22
  • So how can I fix it ? I obviously don't have JDK on target server. I tried copying tools.jar next to java, but still no success. – Nati Mar 01 '16 at 21:56
  • You don't ? Err.... ok. How about I patch it so that it will look for tools.jar in the same directory as the jmxlocal jar ? – Nicholas Mar 01 '16 at 22:25
  • Patch added. Try it now (from source) – Nicholas Mar 01 '16 at 22:34
  • I don't see any commits to the repository on github... are you sure you patched it ? – Nati Mar 01 '16 at 22:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105059/discussion-between-nati-and-nicholas). – Nati Mar 01 '16 at 22:46
  • @Nati; Can't get to chat from work network. Added the heapdump built in. Implemented new command line handler. Much cleaner and easy to extend. Command: java -jar target/jmxlocal-1.0-SNAPSHOT.jar -jmxurl service:jmx:attach:///29431 -psx -c hdump Result: Completed Successfully. Result:/tmp/29431-heap-2016-03-02-110323.dmp. msg me through github if you have any issues. – Nicholas Mar 02 '16 at 16:04