VisualVM and VS Code Integration

GraalVM Tools for Java extension provides integration with VisualVM, which is the all-in-one Java (and polyglot) monitoring and troubleshooting tool. This brings the visual Java tooling to VS Code.

VisualVM and VS Code Integration

It you go to View, then Command Palette, and search for “VisualVM”, the following actions related to VisualVM are available:

VisualVM Commands available from Command Palette

To start monitoring from VS Code, do the following:

  1. Click Gr in the activity panel, install the latest GraalVM release, and activate it, as described here.
  2. Run your Java application (with or without debugging), the process ID will be discovered automatically. You can see that in the VisualVM pane in VS Code:

    Process ID detected

    While the project is starting, the Process node in VisualVM pane displays the project name with a “pid pending” label. Once the project process starts, the Process node is updated to show its process ID (PID).

    Note: VisualVM in VS Code screens the system and searches for the project process during the project startup.

  3. Click the play button:

    Open VisualVM from VS Code

    • It starts VisualVM if no PID is selected in the Process node.
    • It starts VisualVM and opens the PID if selected in the Process node.
  4. If you continue and invoke the actions like Thread dump, Heap dump, CPU sampler, Memory sampler, JFR, the prompt will ask you which process to monitor by VisualVM:

    Select Java process to monitor by VisualVM

You can manually request checking Java processes that run concurrently by clicking the loop icon in the VisualVM:Process field. It starts reading Java processes, and then asks to select which process to monitor.

By default, VisualVM should not open when a Java process is started, but you can configure another behavior. In the VisualVM pane, click the edit button and select the action to perform when the project is started.

Select action to perform when Java project is started

VisualVM Pane

The VisualVM pane provides the following actions and features:

Start VisualVM

The action to start the VisualVM tool is bundled with an active GraalVM installation. If a process has already been selected for monitoring, VisualVM opens the process view at the defined tab. If a process selected for monitoring is not running any more, invoking the action just clears the process selection and shows a notification. Subsequent action invocation starts VisualVM again without a process context.

More Actions… -> Preselect [Overview/Monitor/Threads/Sampler] view

The choices to define which tab will be selected when opening the process view.

VisualVM Pane in VS Code

More Actions… -> Bring VisualVM window to front

When selected, the VisualVM window is made the topmost window every time an action is invoked on VisualVM. It may have no effect in certain window systems.

More Actions… -> Enable Go to Source integration

Only available when there is a project open in VS Code. When selected, the VisualVM is configured to open source code back in VS Code editor on Go to Source action invocation in various VisualVM views. The VisualVM Options | Sources settings are populated by the current project’s source roots, active GraalVM installation sources, and VS Code is set to be the sources viewer.

Note that Go to Source in VisualVM will open sources defined in the VS Code project and the active GraalVM sources. Sources of the project libraries and other dependencies cannot be opened from VisualVM. Unselect the Enable Go to Source integration choice to enable manual source roots and sources viewer definition in VisualVM Options | Sources settings.

Note: Go to Source action is currently not available in some VisualVM views and has some limitations for certain Java language features. See Sources Support for details.

Process

A node to track the process selected for monitoring by VisualVM. By default no process is selected, showing the not selected value. A process can be selected for monitoring using the Select process action available for the Process node, by invoking any action which requires the process selection, using the Open Process In VisualVM in Command Palette, or by starting the project using the special Launch VisualVM & Java 8+ Application launch configuration.

When selecting a process for monitoring, list of local Java processes started by the current user is displayed (populated using the jps tool from the active GraalVM). For each process the main class is displayed if available, along with the process ID. If the process is configured to display a custom name in VisualVM (-Dvisualvm.display.name=CustomName), the custom name is displayed instead of the main class. Each process item also shows a fully qualified main class name together with the arguments passed to the process, if available.

Once the selected process has terminated, the node is cleared on first invocation of either Start VisualVM or any action which requires the process selection.

When Started

A node to define the action to perform when the process is started. This feature requires starting the project using the Launch VisualVM & Java 8+ Application launch configuration. The following choices are available:

By default, the Open process choice is selected. This means that once the project process is started (using the Launch VisualVM & Java 8+ Application launch configuration), VisualVM is started as well, the project process is opened, and defined tab selected. Use the Configure action to change the default choice.

Thread Dump

A node and action to take and display a thread dump from the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Take thread dump action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

Heap Dump

A node and action to take and display a heap dump from the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Take heap dump action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

CPU Sampler

A node and actions to start, snapshot, and stop the CPU sampling session using the defined settings for the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the CPU sampler actions just clears the process selection and shows a notification. Subsequent actions invocation will request another process selection.

The following actions are available for the CPU sampler:

Memory Sampler

A node and actions to start, snapshot, and stop the memory sampling session using the defined settings for the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Memory sampler actions just clears the process selection and shows a notification. Subsequent actions invocation will request another process selection.

The following actions are available for the Memory sampler:

The Memory sampler node can be further expanded to access the memory sampling settings. The following settings can be configured using the Configure action:

JDK Flight Recorder

A node and actions to start, dump, and stop the JDK Flight Recorder (JFR) session using the defined settings for the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the JFR actions just clears the process selection and shows a notification. Subsequent actions invocation will request another process selection.

The following actions are available for the JFR:

The JFR node can be further expanded to access the flight recorder settings. The following settings can be configured using the Configure action:

Note: If the current active GraalVM installation is older than GraalVM 21.2.0, only the Process node and When started node are available in the VisualVM pane. The When started node only provides Do nothing and Open process choices. More actions… menu does not provide the Bring VisualVM to front and Enable Go to Source integration actions. Make sure to use the most recent GraalVM release to access all features.

Available Commands

The following quick actions related to VisualVM become available if you install active the most recent GraalVM in VS Code:

VisualVM Commands available from Command Palette

VisualVM: Start VisualVM

Start the VisualVM tool bundled with the active GraalVM installation. If a process has already been selected for monitoring, VisualVM opens the process view at the defined tab. If the process selected for monitoring is not running any more, invoking the action just clears the process selection and shows a notification. Subsequent action invocation starts VisualVM again without a process context.

VisualVM: Open Process In VisualVM

Request process selection and opens the selected process in VisualVM at the defined tab.

VisualVM: Take thread dump

Take and display a thread dump from the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Take thread dump action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

VisualVM: Take heap dump

Take and display a heap dump from the process selected for monitoring. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Take heap dump action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

VisualVM: Start CPU sampling

Start a new CPU sampling session for the process selected for monitoring, if not already running, using the defined settings, and open the process in VisualVM at the Sampler tab. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Start CPU sampling action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

VisualVM: Start memory sampling

Start a new memory sampling session for the process selected for monitoring, if not already running, using the defined settings, and open the process in VisualVM at the Sampler tab. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Start memory sampling action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.

VisualVM: Start flight recording

Start a flight recording session for the process selected for monitoring, if not already running, using the defined settings, and opens the process in VisualVM at the defined tab. Process selection will be requested if no process has been selected for monitoring yet. If the process selected for monitoring is not running any more, invoking the Start flight recording action just clears the process selection and shows a notification. Subsequent action invocation will request another process selection.