Every Java developer is familiar with javac
for compiling, java
for running, and probably jar
for packaging Java applications. However, many other useful tools come installed with the JDK. They are already on your computer in your JDK’s bin/ directory and are invokable from your PATH. It’s good to get acquainted with some of these tools so you know what’s at your disposal:
jps
ps aux | grep java
to find the running JVMs, you probably just want to run jps
. This dedicated tool lists all the running JVMs, but instead of showing you a lengthy command with CLASSPATH
s and arguments, jps
simply lists the process ID and the application’s main class name, making it far easier to figure out which process is which. jps -l
will list the fully qualified main class name, jps -m
will show the arguments passed to the main method, and jps -v
will show all the arguments passed to the JVM itself.javap
javap <class file>
to see that class file’s fields and methods, which can often be very enlightening for understanding what code written in JVM-based languages such as Scala, Clojure, or Groovy is turned into under the hood. Run javap -c <class file>
to see the complete bytecode of those methods.jmap
jmap -heap <process id>
will print a summary of the JVM process’s memory space, such as how much memory is being used in each of the JVM’s memory generations, as well as the heap configuration and type of GC being used. jmap -histo <process id>
will print a histogram of each class in the heap, how many instances there are of that class, and how many bytes of memory are consumed. Most critically, running jmap -dump:format=b,file=<filename> <process id>
will dump a snapshot of the entire heap to a file.jhat
jhat <heap dump file>
will take the file generated by jmap
and run a local web server. You can connect to this server in a browser to explore the heap space interactively, grouped by package name. The “Show instance counts for all classes (excluding platform)” link shows only instances of classes outside of Java itself. You can also run “OQL” queries, allowing you to query the heap space via SQL-esque syntax.jinfo
jinfo <process id>
to see all system properties the JVM loaded with and JVM command-line flags.jstack
jstack <process id>
will print stack traces for all current Java threads running in a JVM.jconsole
and jvisualvm
jshell
Many of these tools can run not only locally but against JVM processes running on remote machines as well. These are only some of the useful programs you already have installed; take some time to see what else is in your JDK’s directory of executables and read their man pages—it’s always handy to know what tools are in your toolbelt.