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
