Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

VisualVM for Java Development

May 09, 2011

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.

Getting Started

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:

  1. Choose the VisualVM Tools menu, and select the Plugins menu option
  2. Click the Available Plugins tab and select the plugin you want to install
  3. 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).

Select app

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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.