Hidden in the bin directory of the JDK distribution are some little known, but useful tools. In this article we’ll look at the basics of using jconsole to monitor threads, memory usage and object creation.
Setting Your App Up to Be Monitored
If you’re running Java 6, you’re all good to go. The option to allow your application to be monitored is on by default. Skip to the section entitled Launching JConsole and collect 100 points for being on the cutting edge.
If you’re on Java 1.42 or earlier, do not pass go, or collect $200. Time to upgrade. JConsole requires Java 5 or later.
If you’re on Java 5, then you need to start the application that you want to monitor while passing an extra flag to the JVM to enable monitoring. For our example we’re going to launch the SwingSet demo that comes with the JDK. If you’re on Windows, Linux or Solaris it’s usually located in %JDK_HOME%/demo/jfc/SwingSet2. If you’re on Mac OS X, it’s usually in /Developer/Examples/Java/JFC/SwingSet2.
Launching the Application to be Monitored
If you’re launching the application from the command line, simply add -Dcom.sun.management.jmxremote=true right after the java command. Like this:
java -Dcom.sun.management.jmxremote=true -jar SwingSet2.jar
If you’re launching the application from within Eclipse, select Run -> Run…, click on the Arguments tab and specify -Dcom.sun.management.jmxremote=true in the VM arguments field.
If you’re launching the application from within NetBeans, select the Project, right-click and select Properties, select Run node and specify -Dcom.sun.management.jmxremote=true in the VM Options field.
JConsole comes with the JDK (not the JRE) and can be found in %JDK_HOME%/bin on Windows, Solaris and Linux and in /System/Library/Frameworks/JavaVM.framework/Versions/1.5/Commands on Mac OS X. To launch it, open a terminal or command window, change to the directory containing it and execute jconsole. This should open a window that looks like this:
Click the Connect button to begin monitoring your app. As you can see from the screenshots below you can can view summary information, threads usage, memory usage, current and total classes loaded.
The information provided by jconsole is invaluable when tracking down thread creep or memory leaks. It works great to monitor an app while putting it through its paces or when leaving it to run over the weekend to ensure there aren’t any uphill graphs. It’s easy, free and doesn’t require any extra work during application development.
Technorati Tags: java, netbeans, performance, eclipse, testing