JVM tools

Posted by

JCM2

Java ships a bundle of nice tools for getting to know what´s going on with you JVM. A lot of these are probably - despite their obvious usefulness - little used by most developers. Hardy Ferentschik from JBoss made an effort to bring some of these to light again with a talk called "Using the right tools to get back into the driver's seat of your JVM" at JavaZone 2014

For me, this was a nice reminder to actually use the tools provided with your JVM. Here is a short overview of some of the tools he presented

jps

    
ps -ef | grep java


Does this seems familiar? The "jps" command replaces this command, and displays all the running JVMs. It has been around since java 1.5. The default output with no options are not very helpful, but I am using it as a function setting "-lvm" as default and piping to some sed-expressions:

This will render a nicely formatted output

Remember that this has to be run as the user that launched the process.

jstat

The jstat tool could be used to monitor heap and garbage-collect behavior. A typical gc monitoring could look like this:

For a nice overview of using the jstat to monitor gc-behaviour have a look at this blog: http://www.cubrid.org/blog/dev-platform/how-to-monitor-java-garbage-collection/

jmap

The jmap tool shows heap memory details of a given process:

This tool would typically be used to generate a heap-dump file to be analyzed. To generate a heap-dump, do the following:

This dump could be analyzed using our next tool to consider:

jhat

The jhat tool (Java Heap Analysis Tool) parses a java heap dump file and launches a webserver and offers some lightweight analyzing tools for the state of the JVM.

The webserver will then be available at localhost:7000. Here, you will be see a list of all classes in your app, not very useful at first glance. Scroll down the page to the interesting section at the bottom of the page, the "Other queries" section:

other queries

The most useful here is probably "Show instance count for all classes (excluding platform)", where an unusual high number of certain classes indicates trouble. Also the OQL could be helpful. For a more detailed walkthrough of the jhat analyzer check out this blog: http://petermodzelewski.blogspot.no/2013/06/short-jhat-tutorial-diagnosing.html

jmc

The Java Mission Control is the most advanced tool in the JVM toolkit. It was added in a Java 7 JDK update (7u40). Its free to use in development environment, but a fee applies if using in production. This example is made use karaf appserver, so a bit of work has to be done to get it up and running. First, I add this options JAVA_OPTS to enable flight control in JMC;

export JAVA_OPTS="-XX:+UnlockCommercialFeatures -XX:+FlightRecorder"


Then, since we need a user/password to connect to the karaf JMX, I create a new connection with custom connection parameters:

JMX connection

 Then I start a new Flight Recording, configured to last for two minutes:

Flight Recording

 Then we are able to analyze what was going on in the JVM at the time of the recording, e.g CPU and method invocation statistics:

Record1

Recording2 

The JMC is a nice, lightweight alternative to more heavyweight JProfiler or YourKit. While not as rich in features, its easy to setup and free for development environment.

 

The full talk from Hardy Ferentschik at JavaZone2014 is available at http://vimeo.com/105851443

 

Comments