liveBook · Manning

VisualVM

As explained in Troubleshooting Java, Second Edition

VisualVM is a free Java monitoring and profiling tool that provides runtime information about applications and the JVM.

VisualVM is a free Java monitoring and profiling tool for examining an application's runtime behavior and JVM resource use. It helps investigate CPU consumption, memory growth, live threads, memory allocation, thread dumps, and heap dumps. section 5.0 section 5.2

Use VisualVM when an observable symptom needs investigation rather than guesswork. Its Monitor and Threads tabs provide clues about abnormal CPU or memory use, garbage-collection activity, surviving threads, and possible deadlocks. section 5.2.2 section 8.1.1

A practical investigation proceeds from broad runtime signals to focused evidence:

  • Install VisualVM, configure visualvm_jdkhome for a Java 8-or-later JDK, start the application, and select its local process. section 5.2.1
  • Check CPU, garbage collection, and memory widgets for sustained or abnormal resource use. section 5.2.2
  • Inspect the Threads tab when CPU use suggests zombie threads, concurrency problems, or a frozen application. section 5.2.2 section 8.1.1
  • Use memory sampling to identify object types consuming space or appearing in unusually large numbers, then use memory profiling for code-level detail. section 9.1
  • Capture a thread dump for a frozen process or a heap dump for retained objects and suspected memory leaks. section 8.1.1 section 10.1.2

Diagnosing a memory leak

An application repeatedly adds Cat instances to a static ArrayList without removing references. VisualVM shows continuously increasing used memory and intensified garbage collection; the process eventually throws OutOfMemoryError. A heap dump can reveal the retaining ArrayList and its location in application code. section 5.2.3 section 10.2

Diagnosing unsafe concurrency

Two producer threads and two consumer threads concurrently modify an ArrayList without synchronization. VisualVM exposes high CPU use and thread status; after synchronized blocks are added, CPU consumption falls and thread execution becomes visibly coordinated. section 5.2.2

visualvm_jdkhome="C:\Program Files\Java\openjdk-17\jdk-17"

Use the VisualVM JDK-home configuration example when the tool fails to start or connect to a local Java process. section 5.2.1

How Troubleshooting Java, Second Edition covers it

How deep it goes
Walks from installation through runtime widgets, sampling, instrumentation, thread dumps, and heap-dump inspection, while deferring root-cause heap analysis to detailed dumps.
Examples
Demonstrates unsafe producer-consumer threads, an accumulating Cat list, a million-object endpoint request, and frozen threads entering deadlock.
What it assumes
Assumes a running Java application and configured JDK, while introducing profiling observations and diagnostic questions from the beginning.
What you can do after
Diagnose/debug with it by correlating runtime graphs, thread states, sampled objects, thread dumps, and heap references with application behavior.
How it connects
Connects JVM monitoring with garbage collection, memory leaks, memory profiling, thread dumps, heap dumps, race conditions, and deadlocks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.