Mastering JVisualVM: Detect and Analyze Java Memory Leaks & Remote Tomcat Monitoring
JVisualVM, bundled with JDK, provides a visual interface to monitor JVM threads, memory, and CPU usage, allowing installation of plugins for GC, thread, and memory analysis, demonstrating how to simulate memory leaks, capture heap dumps, compare snapshots, trace object references, and set up remote Tomcat monitoring via JMX.
JVisualVM Introduction
VisualVM is the profiling sub‑project of NetBeans and is included in JDK 6u7 and later. It can monitor threads, memory usage, CPU time of methods, and view objects that have been garbage‑collected or allocated.
The tool provides a visual UI for inspecting local or remote Java applications, capturing JVM data and saving it for later analysis or sharing.
After launching jvisualvm.exe you can select local or remote JVMs similar to jconsole.
Main Interface
VisualVM’s functionality can be extended with plugins that focus on GC, memory, threads, etc.
How to Install Plugins
1. Choose “Tools” → “Plugins”. 2. In the “Available Plugins” tab, check the desired plugin and click “Install”. 3. Follow the installation wizard.
Simulating a Memory‑Leak Example
1. Define a static HashMap as a cache.
2. In a loop create objects and put them into the map.
Code example:
import java.util.HashMap;
import java.util.Map;
public class CyclicDependencies {
// cache object
private static final Map map = new HashMap();
public static void main(String args[]) {
try {
Thread.sleep(10000); // give time to open VisualVM
} catch (InterruptedException e) {
e.printStackTrace();
}
// add objects to cache
for (int i = 0; i < 1000000; i++) {
TestMemory t = new TestMemory();
map.put("key" + i, t);
}
System.out.println("first");
// additional loops omitted for brevity
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("qqqq");
}
}3. Configure JVM parameters:
-Xms512m
-Xmx512m
-XX:-UseGCOverheadLimit
-XX:MaxPermSize=50m4. Run the program and open VisualVM to monitor.
Analyzing the Memory Leak with JVisualVM
Open the “Visual GC” tab to see heap usage after each print statement (first, second, third, forth). Compare heap dumps taken at “second” and “forth” to observe the growth of TestMemory instances.
Use the “Sampler” tab to capture heap snapshots, then compare the two dumps. The comparison shows an increasing number of TestMemory objects, indicating a leak.
To view object references, right‑click the TestMemory class and select “Show in Instances”. The instance view reveals that the objects are retained by the static HashMap in CyclicDependencies.
Remote Monitoring of Tomcat with JVisualVM
Modify catalina.sh to add the following JVM options:
-Djava.rmi.server.hostname=192.168.122.128
-Dcom.sun.management.jmxremote.port=18999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
Start JVisualVM, add a remote host by IP, then create a JMX connection using the configured port.
After connecting, you can monitor the remote Tomcat JVM just like a local one.
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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
