In the previous article, we navigate through jmap tool to analyze application performance, jmap is a console based which did not have a visual interface to analyze the data, In this article, we will understand one more profiling tool that is VisualVM.

Java VisualVM is a tool that provides a visual interface for viewing detailed information about Java applications while they are running on a Java Virtual Machine (JVM), and for troubleshooting and profiling these applications.

Various optional tools, including Java VisualVM, are provided with Sun’s distribution of the Java Development Kit (JDK) for retrieving different types of data about running JVM software instances. For example, most of the previously standalone tools JConsole, jstat, jinfo, jstack, and jmap are part of Java VisualVM. Java VisualVM federates these tools to obtain data from the JVM software, then re-organizes and presents the information graphically, to enable us to view different data about multiple Java applications uniformly, whether they are running locally or on remote machines.

With help of VisualVM we can generate and analyze heap dumps, track down memory leaks, browse the platform’s MBeans and perform operations on those MBeans, perform and monitor garbage collection, and perform lightweight memory and CPU profiling.

VisualVM was introduced starting from Standard Edition of JDK 6. It is present as a tool till JDK 8. From Java 9 onward VisualVM is a separate project and no more part of JDK took-kit.

Starting Java VisualVM

Once we have installed an appropriate JDK version, navigate to your JDK software’s bin directory and double-click the Java VisualVM executable. On Windows this directory will be the following:

C:\Program Files\Java\jdk1.6.0_update-number\bin

Alternatively, navigate to your JDK software’s bin directory and type the following command at the command (shell) prompt:


Java VisualVM Windows

When we start Java VisualVM, the main Java VisualVM window opens. The main Java VisualVM window is divided into two windows.

VisualVM Windows
VisualVM Windows
Applications Window

The Applications window opens in the left side of the main Java VisualVM window when we launch the application. The Applications window is the main point of entry for exploring the details of running applications. The Applications window uses a tree structure to enable us to quickly view the applications running on the local machine and any connected remote machines. We can also access core dumps (Solaris/Linux) and saved snapshots from the Applications window.

Main Window

Detailed information about applications is displayed in the main window. When you view data about an application, each application is represented by a tab in the main window that enables you to easily view and navigate the data. Saved thread dumps and heap dumps are also opened in the main window.

Profiling with Local Application

Let’s understand profiling with one of local application. Suppose we have attached program which is creating a 10000 objects.

1. The overview view:
The Overview View
The Overview View

The Overview tab displays the following general information about the application and the runtime environment.

  • PID – Process ID of the application
  • Host – The location of the system that the application is running on.
  • Main class – The class containing the main method.
  • Arguments – Displays any options passed to the application on startup.
  • JVM – JDK version of the JVM software.
  • Java Home -The location of the JDK software.
  • Heap dump on OOME – This displays the status of the Heap Dump on OOME option. When enabled, a heap dump is taken when the application encounters an OutOfMemory exception. You can enabled or disable the option in the application popup menu.
2. The monitor view:
The monitor view
The Monitor View

Monitoring data is displayed in the following graphs:

  • Heap – The Heap graph displays the total heap size and how much of the heap is currently used. These are the figures that a Java technology-based application (Java application) can obtain using the java.lang.Runtime.totalMemory() and java.lang.Runtime.freeMemory() calls.
  • PermGen – The PermGen graph displays changes in the permanent generation area over time. The permanent generation is the area of the heap where class and method objects are stored. If an application loads a very large number of classes, then the size of the permanent generation might need to be increased using the -XX:MaxPermSize option. Classes The Classes graph displays an overview of the total number of loaded classes and shared classes. Threads. The Threads graphs display an overview of the number of live and daemon threads in the application’s JVM. You can use Java VisualVM to take a thread dump if you want to capture and view exact data on application threads at a specific point in time.

The Monitoring tab contains buttons that enable you to perform the following actions:

  • Perform GC – Click Perform GC to perform garbage collection immediately.
  • Heap Dump – Click Heap Dump to take a heap dump. When you take a heap dump, a tab displaying the heap dump opens in the application tab. A node for the heap dump appears under the node for the application in the Applications window.
3. The Threads View:

The information displayed in the Threads tab is based on Java Management Extensions (JMX). The Threads tab is visible if Java VisualVM can make a JMX technology-based connection (JMX connection) with the target application and retrieve JMX instrumentation from the Java Virtual Machine (JVM).

4. The Sampler View:
Sampler View
Sampler View
5. The Profiler View:
Profiling View
Profiling View

By default the profiling tool is not running until you are ready to profile the application. You can choose from the following profiling options:

CPU Profiling

This profile command returns detailed data on method-level CPU performance (execution time), showing the total execution time and number of invocations for each method. When analyzing application performance, Java VisualVM instruments all of the methods of the profiled application. Threads emit the “method entry” event when entering a method and generate the corresponding “method exit” event when exiting the method. Both of these events contain timestamps. This data is processed in real time.

Memory Profiling

Choose this to analyze the memory usage of the application. The results display the objects allocated by the application and the class allocating those objects.

Remote Application Profiling

Java VisualVM is able to access and display data about applications running on remote hosts. After connecting to a remote host, Java VisualVM can display general data about the application’s runtime environment and can monitor memory heap and thread activity.

To add a remote host, right-click the Remote node in the Applications window, choose Add Remote Host and type the host name or IP address in the Add Remote Host dialog box.

Remote Host Connection
Remote Host Connection

Note:To retrieve and display information on applications running on the remote host, the jstatd utility needs to be running on the remote host. To start the jstatd utility, type jstatd on the command line. The jstatd utility is included as part of Java Development Kit (JDK) version 6.

That’s all for VisualVM, Hope you like it, Keep reading and sharing !! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *