Mastering JVM Memory Analysis with VisualVM: A Hands‑On Guide
This tutorial walks Java developers through using VisualVM to monitor JVM threads, memory, and CPU, demonstrates how to create a memory‑leak scenario, generate heap dumps, and interpret VisualVM and Visual GC data to pinpoint memory‑leak root causes.
Introduction
If you have been developing Java for years but never used a JVM analysis tool, you are missing a crucial skill for troubleshooting and performance tuning. This article uses the JDK‑bundled VisualVM to introduce JVM analysis, illustrating concepts with a deliberately created memory‑leak example.
What Is VisualVM?
VisualVM is a NetBeans profiling sub‑project included in JDK 6u7 and later. It can monitor threads, memory usage, CPU time per method, and inspect objects in the heap, including those already garbage‑collected. After setting the classpath correctly, launch it with jvisualvm. VisualVM supports both local and remote monitoring; remote setup is slightly more complex.
Key VisualVM Screens
Overview
Selecting a Java process shows an Overview tab with JVM version, dump batch, and JVM parameters—useful for verifying runtime configuration. A common mistake is placing JVM options after the -jar argument, which makes them ineffective (e.g., java -jar app.jar -Xms256m -Xmx512m vs. java -Xms256m -Xmx512m -jar app.jar).
Monitor
The Monitor tab displays CPU usage, heap and Metaspace consumption, thread activity, and class loading statistics. It helps track heap growth and Metaspace expansion over time.
Heap Dump
Clicking “Heap Dump” creates a dump file at the current timestamp. The dump’s Summary shows the file path and time. In the Classes view you can explore memory consumption per class; double‑click a class to see instance counts.
Creating a Memory‑Leak Scenario
We write a simple program that continuously adds objects to a static Map, optionally sleeping to give VisualVM time to capture data. The code is compiled with limited heap size ( -Xms128m -Xmx128m) to accelerate the leak.
public class MemoryLeakTest {
private static final Map<String, TestMemory> CACHE_MAP = new HashMap<>();
public static void main(String[] args) throws Exception {
Thread.sleep(10000); // give VisualVM time to attach
for (int i = 0; i < 1_000_000; i++) {
CACHE_MAP.put("key" + i, new TestMemory());
}
System.out.println("-------1------");
Thread.sleep(10000);
for (int i = 0; i < 1_000_000; i++) {
CACHE_MAP.put("key" + i, new TestMemory());
}
}
}
class TestMemory { }Installing the Visual GC plugin (via Tools → Plugins) adds detailed GC visualisation.
Analyzing the Leak
Running the program and observing VisualVM, the heap usage curve rises sharply while Metaspace also grows. The Visual GC view shows the heap layout (young generation, Eden, Survivor spaces, old generation, Metaspace) and frequent Full GC events (e.g., 3850 Full GCs in 20 minutes), indicating severe pressure.
In the Classes tab, the TestMemory class shows about 1 million instances, confirming a leak caused by the static cache. Double‑clicking the class reveals instance details.
Conclusion
The article demonstrates how VisualVM can be used for real‑time JVM monitoring, heap dump generation, and memory‑leak diagnosis. By reproducing a leak, inspecting the Overview, Monitor, and Classes tabs, and using Visual GC, developers gain practical skills to locate and fix memory‑related issues in production Java applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
