VisualVM for Java Development
When I was a Windows programmer, my group was steadfast in running and testing with the debug version of the Windows kernel before releasing any software. Doing so seemed always to reveal many critical issues, such as memory leaks and potential misuse of Windows API calls. One of the benefits of being a Windows developer has been Microsoft's support in terms of tools and online help.
However, I'm continually surprised to see so many Java developers unaware of, or just unwilling to use, the many tools available to them as well. Two of the tools that I've relied on through the years are VisualVM and VisualGC, which are now available both as standalones and within NetBeans and Eclipse. Although Oracle provides these as a separate download, they are included with the latest versions of the Java SE 6 JDK. Look in the bin directory of your local installation to start these tools in standalone mode. If for some reason you don't have them, you can download via the aforementioned URL.
When started, VisualVM lists all running JVMs on a system (local and remote, if you'd like), and allows you to do the following with them:
- View configuration information, such as the JVM version, application and JVM flags used at startup, libraries/JARs imported, process ID, and other system properties that the JVM is running with.
- Dynamically monitor (with live updates) the Java application as it runs, including garbage collection activity, heap allocation activity, loaded classes, and running threads.
- Monitor CPU usage per thread within your application, and visually identify deadlocks, contention, and other potential locking issues.
- Monitor memory usage to watch allocations in action, or take heap dumps at specific moments of time and then compare them to locate potential memory leaks.
- Profile your application to identify the largest consumers of memory and CPU within your application (to help you locate hot spots for further debugging and optimization).
Are you experiencing JVM shutdowns, crashes, or core dumps? You can use VisualVM to analyze the output of the dump or shutdown to help determine what led up to it.
Before you begin, I strongly urge you to look at the list of available plugins, and at least install the VisualGC plugin. This tool gives you incredibly detailed insight into what's happening within the garbage collector and your application's heap usage. Garbage collection can be a source of huge performance loss, latency issues, and overall unpredictable behavior if your application has memory-related problems or is in need of tuning.
To install a plugin from the VisualVM Plugins Center follow these steps:
- Choose the VisualVM Tools menu, and select the Plugins menu option
- Click the Available Plugins tab and select the plugin you want to install
- Click the Install button
To begin debugging with VisualVM, simply start your Java application, then run "visualvm" from the command line. Make sure your JDK bin directory is either the current directory or in the path. Once VisualVM starts, a list of local Java VM processes will be shown in the upper left of the display under the header "Local" (see the figure below).
You can also connect and monitor remote applications. To do so, ensure that "stated" is running on the remote host, then start the application you want to monitor there (the order doesn't matter). Within VisualVM, to explicitly add the remote host, select the Plugins option within the Tools menu, and enter the host information (name or IP address and label). After the connection is made (check for local firewall issues or typos if it fails), you should see a list of Java processes running on the remote host under the Remote header (see the figure above).
You can begin with the dynamic, live-updating monitor screen for your application (see the figure below). This screen provides a cursory overview of CPU usage, garbage collection activity, heap usage, classes loaded and instantiated, and threads that are active.
This is only an overview; to get to the details, you need to click on the tabs along the top labeled Threads, Sampler, Provider, and VisualGC.