JavaOne 2012: Diagnosing Your Application on the JVM

It was worth attending Staffan Larsen's (Oracle Java Serviceability Architect) presentation "Diagnosing Your Application on the JVM" (Hilton Plaza A/B) just for learning of the new jcmd command-line tool provided with Oracle's JVM 7. The rest of the presentation was "bonus" for me, which was nice for the last session I attended on Wednesday of JavaOne 2012.

The Oracle HotSpot JDK provides jcmd, a command-line tool designed to be both backwards compatible and forward adaptable for future versions of Java. It is designed to support new tools and features that come with new SDKs in a standardized approach. The following screen snapshot shows it used for most basic jps-like functionality (Larsen mentioned jps almost as briefly as I just did and referred to jcmd as "like jps but more powerful").

During the course of his presentation, Larsen needed to convert a decimal number (pid?) to its hexadecimal representation for comparing it to the output of another tool. He used the handy printf "%x\n" <pid> command to get the hexadecimal representation of the pid.

NOTE: I will be adding more details in this section with code/script examples based on what Larsen showed in his presentation. Look for updates here in the next few days.

Larsen demonstrated use of VisualVM to compare two heap dumps and browse a heap dump. He also demonstrated the VisualVM Profiler.

Larsen moved from the tools previously covered aimed at running JVMs to tools that can be used to analyze JVM core files. He returned to jstack to analyze contents of the core file.

Larsen talked about remotely accessing JVM information via JMX and tools like jconsole and jvisualvm. He demonstrated that jcmd can be used to start JMX exposure as well: ManagementServer.start "with a bunch of parameters." Larsen feels that VisualVM and JConsole would use ManagementServer.start rather than Attach API if implemented today.

jstat can also connect to daemon remotely through use of jstatd. There is no encryption or authentication with jstatd.

jps and jcmd find what's running on system using "well-known file for each JVM": /hsperfdata_<user>/<pod> This file is created on JVM startup and deleted on JVM shutdown. Unused previous files are deleted on startup, so jps and jcmd, as Java programs themselves, will clean these old ones up.

The Attach API "allows sending 'commands' for executionin the JVM," but only works on local machine and for current/same user. This is what jcmd and jstack use. Larsen then went onto explain the different mechanics of using the Attach API for Linux/BSD/Solaris (uses temporary file creation) versus Windows (uses code injection). I employed the Attach API in my post Groovy, JMX, and the Attach API.

Diagnostic commands are "helper routines inside the JVM" that produce "text output." They can be executed via the jcmd utility (and soon via JMX). They each have a self-describing facility: jcmd PerfCounter.print to see the raw contents.

Larsen showed an informative table comparing "communicating with the JVM" approaches: attach, jvmstat, JMX, jstatd, and Serviceability Agent (SA). The SA "should be used as a last resort ('typically for a JVM that is hung')" and uses a "debugger to read information."

Larsen transitioned to talk of future tools. He started this portion of the presentation with coverage of Java Flight Recorder. Java Flight Recorder is a "JVM-built-in profiler and tracer" with "low overhead" and is "always on." Other coming tools are Java Mission Control ("graphical tool providing very detailed runtime monitoring details"), more diagnostic commands for jcmd ("eventually replacing jstack, jmap, jinfo" for various reasons), JMX 2.0 ("something we're picking up again; it was started a very long time ago"), improved logging for JVM (JVM Enhancement Proposal [JEP] 158), and Java Discovery Protocol (anticipating forthcoming JEP for this).

One question asked was if one could see MBeans in VisualVM as can be done in JConsole. As I've blogged on, there is a VisualVM plug-in for doing just that.

Although I felt somewhat comfortable with the Oracle HotSpot JDK command line tools, I was unfamiliar with jcmd and appreciated Larsen's coverage of it. I learned some other things along the way as well. My only complaint is that Larsen's presentation (especially the demonstration) was so rapid fire and so content-rich that I wish I could see it again.

A related (but older) presentation with some of the same content is available at

Original posting available at (Inspired by Actual Events)