Using Arthas to Diagnose High CPU Usage in a Java Application
This tutorial demonstrates how to employ the open‑source Java diagnostic tool Arthas to quickly locate and analyze a high‑CPU problem in a running JVM by leveraging commands such as dashboard, thread, jad, watch, and ognl, complete with code examples and step‑by‑step instructions.
Arthas, an open‑source Java diagnostic tool from Alibaba, offers a more user‑friendly and powerful alternative to built‑in JDK tools, enabling one‑click problem location, class decompilation, and even hot code replacement in production environments.
The guide walks through downloading and starting Arthas:
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jarAfter attaching to the target JVM process, the dashboard command provides an overview of threads, memory, and GC, revealing that the CPU hotspot is not caused by GC but by eight threads, seven of which belong to ForkJoinPool.commonPool .
To inspect the busiest threads, the thread -n 8 command is used:
thread -n 8The output shows that these threads are executing MD5 operations, indicating that a parallel‑stream segment is the culprit. By examining the stack of the main thread, the doTask method of HighCPUApplication is identified as the hotspot.
The jad command decompiles the class for deeper analysis:
jad org.geekbang.time.commonmistakes.troubleshootingtools.highcpu.HighCPUApplicationIt reveals that when the integer argument equals a specific constant, the method performs 10,000 MD5 calculations, causing the CPU load. To discover the constant, the watch command monitors method arguments exceeding 100 ms:
watch org.geekbang.time.commonmistakes.troubleshootingtools.highcpu.HighCPUApplication doTask '{params}' '#cost>100' -x 2The watch output shows the argument value is 0 , suggesting the constant ADMIN_ID is zero. This is confirmed with the ognl command:
[arthas@31126]$ ognl '@org.geekbang.time.commonmistakes.troubleshootingtools.highcpu.User@ADMIN_ID'
@Integer[0]Finally, the tutorial notes that commands like monitor , trace , and watch modify bytecode at runtime, so a shutdown should be executed to restore the original state before exiting Arthas.
Overall, the case study shows how Arthas can locate high‑CPU issues in production without source code or additional logging, and mentions a similar tool, Bistoury, for multi‑machine visual debugging.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.