A Comprehensive Guide to Using Arthas for Java Application Diagnosis and Debugging
This article introduces Arthas, an open‑source Java diagnostic tool from Alibaba, explains its key features, shows how to install it via Maven, and provides step‑by‑step commands for entering the shell, monitoring JVM status, tracing method calls, observing system resources, dynamically redefining code, and connecting to applications using default or Telnet modes.
Arthas is an open‑source Java application diagnostic tool created by Alibaba that enables developers to debug and diagnose running Java applications, offering real‑time JVM status, method call tracing, resource monitoring, and dynamic code modification.
Installation : Add the following Maven dependency to your project:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>arthas-client</artifactId>
<version>3.5.4</version>
</dependency>Using Arthas :
Enter the Arthas shell with ./as.sh and verify the welcome screen.
Check JVM status using $ jvm , which displays process ID, user, host, and other runtime details.
Trace method calls with the trace command, e.g., $ trace com.example.demo.service.UserService getUserById to track all invocations, or $ trace com.example.demo.service.UserService -n 5 for the first five calls with parameters and return values.
Monitor system resources: $ dashboard shows CPU usage, thread count, and memory; $ thread lists thread information; $ gc reports garbage‑collection details.
Dynamically modify code: $ redefine com.example.demo.service.UserService getUserById "return \"Hello Arthas!\";" changes a method’s return value, and $ redefine -c com.example.demo.service.UserService -m addUser --params-string "java.lang.String name, java.lang.Integer age" "logger.info(\"addUser invoked! name:{} age:{}\", name, age); return true;" adds a new method.
Other useful commands include $ sc -d 3 com.example.demo.service.UserService getUserById to view call count in the last three seconds, $ thread -i [threadId] for a specific thread stack, and $ classloader -c com.example.demo.service.UserService to inspect class loading.
Connecting to Applications :
Default connection: $ java -jar arthas-boot.jar automatically attaches to a local Java process, or specify target IP and port with $ java -jar arthas-boot.jar --target-ip 127.0.0.1 --telnet-port 3658 .
Telnet connection: $ telnet 127.0.0.1 3658 then use help inside the shell for command assistance.
In summary, Arthas acts as a powerful “debugging wand” for Java developers, allowing effortless JVM inspection, method tracing, resource monitoring, and live code changes, making troubleshooting faster and more enjoyable.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.