Diagnose Java Memory Leaks in Minutes with Spring Boot Actuator and Arthas
This article explains why traditional heap dump and stack trace analysis are cumbersome, and shows how combining Spring Boot Actuator metrics with the Arthas diagnostic tool provides real‑time monitoring, rapid object inspection, and precise leak root‑cause identification, dramatically speeding up Java OOM troubleshooting.
Why Stack Traces Feel Like Ancient Scripts
When an OOM alarm rings at 3 am, developers often stare at thousands of lines of GC logs and stack traces, trying to locate the leaking object among noisy framework calls, without any trend information, which makes the investigation feel like searching for a needle in a dark warehouse.
Actuator + Arthas: The Dynamic Monitoring Toolbox
Instead of manually parsing massive logs, the combination of Spring Boot Actuator and Arthas turns a flashlight into a thermal imaging drone, giving you both a global view of memory usage and the ability to drill down to the exact leaking instance without restarting the service.
1. Spring Boot Actuator
Actuator provides built‑in health endpoints; the /actuator/metrics/jvm.memory.used endpoint reports heap memory usage. The key metric for leak detection is Heap Memory Usage .
Ensure the spring-boot-starter-actuator dependency is added and the metrics endpoint is exposed (secure it appropriately).
Access
http://your-host:port/actuator/metrics/jvm.memory.used?tag=area:heapto see JSON data such as:
{
"name": "jvm.memory.used",
"description": "The amount of used memory",
"baseUnit": "bytes",
"measurements": [{"statistic": "VALUE", "value": 536870912}],
"availableTags": [{"tag": "area", "values": ["heap","nonheap"]}, {"tag": "id", "values": ["G1 Eden Space","G1 Survivor Space","G1 Old Gen"]}]
}Key value: monitor jvm.memory.used for the Old Gen region; a continuously rising value, even after Full GC, strongly indicates a memory leak.
2. Arthas – Precise Leak Localization
Arthas, an Alibaba open‑source Java diagnostic tool, works on a running JVM without restart, offering dynamic tracing, hot‑update, and method observation.
Start Arthas: java -jar arthas-boot.jar and select the target PID.
Monitor heap trends: dashboard (overall panel) and memory (JVM memory view).
Command 1 – memory: shows a quick heap summary.
Command 2 – vmtool --action getInstances --className <Class> --limit N: lists classes with unusually many instances.
Command 3 – heapdump --live /tmp/dump.hprof: exports a live heap snapshot for offline analysis (e.g., with MAT).
Command 4 – vmtool --action getStatic / ognl: retrieves GC‑Root information for a specific instance.
Command 5 – monitor / watch / trace: dynamically monitors method calls, parameters, return values, and call paths, allowing you to see which method continuously creates objects.
Efficiency Comparison: Traditional vs. Actuator + Arthas
Traditional debugging relies on reactive alerts, repeated dump generation, and manual stack scanning, often taking hours. The Actuator + Arthas combo provides proactive metrics, minute‑level class‑level statistics, and second‑level root‑cause tracing, reducing the whole process to under 20 minutes and improving efficiency by more than five times.
Real‑World Case: Locking the Forgotten Timer in Five Minutes
Use memory to confirm Old Gen growth.
Run
vmtool --action getInstances --className com.xx.cache.LocalCache --limit 100and discover thousands of LocalCache instances.
Obtain the hash code of one instance.
Open the heap dump in MAT, search the hash code, and find a ScheduledThreadPoolExecutor holding a Runnable that repeatedly creates new caches.
Trace the run method of that Runnable with trace to see the leak source.
Identify the faulty scheduled task that creates a new LocalCache every 10 minutes without cleanup, causing the Old Gen to fill.
Fixing the code stopped the memory curve instantly, turning a night‑long debugging marathon into a five‑minute fix.
Conclusion
For production OOM incidents, avoid manual stack parsing; use Actuator to get real‑time memory trends and Arthas to dynamically trace objects and methods without restarting the service. This toolbox can accelerate leak detection by an order of magnitude, freeing valuable time for developers.
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 Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
