How to Use JVisualVM for Memory Leak Detection and Remote Tomcat Monitoring
This guide introduces JVisualVM, explains its core features and plugins, demonstrates how to simulate and analyze memory leaks with sample code, and shows step‑by‑step remote monitoring of a Tomcat server using JMX.
JVisualVM Introduction
VisualVM is a profiling tool bundled with JDK 6u7 and later, providing visual monitoring of threads, memory, CPU time, and garbage‑collected objects. It can inspect local or remote Java applications, capture heap dumps, and save data for later analysis.
Launch jvisualvm.exe from the JDK bin directory; the UI resembles NetBeans and offers both local and remote monitoring.
Installing Plugins
1. Choose Tools → Plugins from the main menu. 2. In the Available Plugins tab, check the desired plugins and click Install . 3. Follow the wizard to complete installation.
Typical plugins include monitoring, thread analysis, and Visual GC.
Key Features
Monitoring dashboard showing CPU, memory, classes, and thread charts.
Thread view similar to jconsole.
Visual GC displays young/old generation usage, GC frequency, and pause times.
Heap dump analysis and many additional plugins for deeper inspection.
Case Study
Preparing a Memory‑Leak Sample
1. Define a static HashMap to cache objects.
2. In a loop, create objects and store them in the map.
3. Use the following Java code:
import java.util.HashMap;
import java.util.Map;
public class CyclicDependencies {
private static final Map map = new HashMap();
public static void main(String args[]) {
try { Thread.sleep(10000); } 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");
try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }
// Repeat with larger loops
for (int i = 0; i < 1000000; i++) {
TestMemory t = new TestMemory();
map.put("key" + i, t);
}
System.out.println("second");
try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }
for (int i = 0; i < 3000000; i++) {
TestMemory t = new TestMemory();
map.put("key" + i, t);
}
System.out.println("third");
try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }
for (int i = 0; i < 4000000; i++) {
TestMemory t = new TestMemory();
map.put("key" + i, t);
}
System.out.println("forth");
try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("qqqq");
}
}4. Configure JVM parameters:
-Xms512m
-Xmx512m
-XX:-UseGCOverheadLimit
-XX:MaxPermSize=50m5. Run the program and open VisualVM to monitor.
Analyzing the Memory Leak with JVisualVM
Open the Visual GC tab and observe heap usage after each printed checkpoint (e.g., "first", "second", "forth"). Screenshots show increasing old‑generation usage without reduction, indicating a leak.
Use the Sampler tab to take heap dumps at the "second" and "forth" points, then compare the dumps. The comparison reveals a growing number of TestMemory instances.
To inspect object references, right‑click the TestMemory class, select "Show in Instances View", and examine the reference chain. The view shows that TestMemory objects are retained by the static HashMap in CyclicDependencies, confirming the leak source.
Remote Monitoring of Tomcat
Edit catalina.sh and add the following JVM options:
JAVA_OPTS="$JAVA_OPTS \
-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, right‑click Remote and choose Add Remote Host . Enter the Tomcat server IP.
Right‑click the newly added host, select Add JMX Connection , and input the JMX port (18999).
Double‑click the connection to begin monitoring the remote Tomcat instance.
References
https://blog.csdn.net/kl28978113/article/details/53817827
https://www.cnblogs.com/ityouknow/p/6437037.html
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
