Video in progress

How do you diagnose a prod server with jcmd?

jcmd is a universal tool that attaches to a running JVM. With this one command I get thread dumps, heap dumps, GC statistics, flags, start a JFR recording and get the native memory report. There is no need to restart the application.

📖 Answer

First I find the process, then I ask it what it can do:

jcmd                       # list of Java processes
jcmd 4321 help             # commands this JVM supports

The ones I use day to day:

jcmd 4321 VM.flags                 # the flags it is actually running with
jcmd 4321 VM.system_properties
jcmd 4321 Thread.print > threads.txt
jcmd 4321 GC.heap_info
jcmd 4321 GC.class_histogram | head -30   # which classes have the most objects
jcmd 4321 GC.heap_dump /tmp/app.hprof
jcmd 4321 JFR.start duration=120s filename=/tmp/app.jfr
jcmd 4321 VM.native_memory summary        # if NMT is enabled

A few practical points:

  • jcmd must be run as the user who owns the process, otherwise it can't attach;
  • in a container you get in through kubectl exec, and if the image has no JDK (only a JRE), jcmd won't be there, so think about that ahead of time;
  • GC.heap_dump and GC.class_histogram stop the world. On a big heap that means seconds, so it is better to move traffic to another instance first.

jmap, jstack and jinfo still exist, but jcmd replaces all of them.