Arthas is a powerful Java diagnostic tool open-sourced by Alibaba in September 2018. It supports JDK 6 and above, provides interactive command-line mode, supports multiple platforms (Linux/Mac/Windows), and features Tab auto-completion. Arthas helps developers quickly diagnose and locate Java application production issues without modifying code or restarting services.
In daily development, when encountering the following Java issues, traditional methods are inefficient:
Arthas provides powerful solutions for these scenarios.
Download and run with a single command:
# Download arthas-boot.jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
# Launch Arthas
java -jar arthas-boot.jar
After startup, Arthas will automatically detect all Java processes on the current machine:
[INFO] arthas-boot version: 3.x.x
[INFO] Process:
* [1]: 35542 (Spring Boot Application)
[2]: 71560 (Tomcat Application)
[3]: 63914 (Other Java Process)
Please choose the target process (1-3, default is 1):
Simply enter the number to attach to the target process.
thread
- View Thread InformationView all threads:
[arthas@35542]$ thread
Find high CPU consumption threads:
[arthas@35542]$ thread -n 3
# Shows top 3 threads with highest CPU usage
View specific thread:
[arthas@35542]$ thread 1
# View thread with ID 1
Detect deadlocks:
[arthas@35542]$ thread -b
# Shows blocked threads and potential deadlocks
sc
- View Class InformationSearch for classes:
[arthas@35542]$ sc *UserService*
# Search for all classes containing 'UserService'
View class details:
[arthas@35542]$ sc -d com.example.UserService
# View detailed information about UserService class
sm
- View Method InformationView class methods:
[arthas@35542]$ sm com.example.UserService
# List all methods in UserService class
View method details:
[arthas@35542]$ sm -d com.example.UserService getUserById
# View detailed information about getUserById method
watch
- Monitor Method ExecutionMonitor method parameters and return values:
[arthas@35542]$ watch com.example.UserService getUserById '{params, returnObj}' -x 2
Monitor method execution time:
[arthas@35542]$ watch com.example.UserService getUserById '{params, returnObj, throwExp}' -x 2 -b -s -e
# -b: before method execution
# -s: after successful execution
# -e: after exception thrown
trace
- Method Call TraceTrace method execution path:
[arthas@35542]$ trace com.example.UserService getUserById
Trace with time threshold:
[arthas@35542]$ trace com.example.UserService getUserById '#cost > 100'
# Only show calls that take more than 100ms
monitor
- Method Execution StatisticsMonitor method execution statistics:
[arthas@35542]$ monitor -c 5 com.example.UserService getUserById
# Monitor for 5 seconds and show statistics
dashboard
- System Overview[arthas@35542]$ dashboard
# Real-time display of system metrics: CPU, memory, GC, threads, etc.
jvm
- JVM Information[arthas@35542]$ jvm
# View JVM version, parameters, memory allocation, etc.
memory
- Memory Information[arthas@35542]$ memory
# View heap/non-heap memory usage details
gc
- GC Information[arthas@35542]$ gc
# View garbage collection statistics
classloader
- View ClassLoader Information[arthas@35542]$ classloader
# View all ClassLoader information
Find classes loaded by specific ClassLoader:
[arthas@35542]$ classloader -c 327a647b
# View classes loaded by specified ClassLoader
mc
and redefine
- Hot Code ReplacementCompile Java source:
[arthas@35542]$ mc /tmp/UserService.java -d /tmp
# Compile UserService.java to /tmp directory
Hot replace class:
[arthas@35542]$ redefine /tmp/com/example/UserService.class
# Hot replace UserService class
Arthas also provides a Web interface for easier use:
# Start with Web Console
java -jar arthas-boot.jar --target-ip 0.0.0.0
Then access http://localhost:8563
in your browser.
# Step 1: View high CPU threads
[arthas@35542]$ thread -n 5
# Step 2: View specific thread details
[arthas@35542]$ thread 42
# Step 3: Trace suspected methods
[arthas@35542]$ trace com.example.service.ProcessService process
# Monitor method execution time
[arthas@35542]$ watch com.example.UserService getUserById '{params[0], returnObj, cost}' -x 2
# Trace method call chain
[arthas@35542]$ trace com.example.UserService getUserById '#cost > 50'
# Detect deadlocks
[arthas@35542]$ thread -b
# View blocked thread details
[arthas@35542]$ thread 28
# Monitor GC situation
[arthas@35542]$ gc
# View heap memory details
[arthas@35542]$ memory
# Monitor object creation
[arthas@35542]$ watch java.util.ArrayList <init> '{params, target}' -x 2
watch
, trace
may have some impact# Set access restrictions
java -jar arthas-boot.jar --target-ip 127.0.0.1 --http-port 8563 --telnet-port 3658
# Exit Arthas
[arthas@35542]$ quit
# Stop Arthas completely
[arthas@35542]$ stop
# Start profiler
[arthas@35542]$ profiler start
# Generate flame graph
[arthas@35542]$ profiler getSamples
[arthas@35542]$ profiler status
# Monitor log output
[arthas@35542]$ logger
# Change log level
[arthas@35542]$ logger --name com.example --level DEBUG
# Use OGNL expressions in watch command
[arthas@35542]$ watch com.example.UserService getUserById '@user.name'
# Get static field value
[arthas@35542]$ ognl '@com.example.Constants@DEFAULT_SIZE'
# Filter by execution time
[arthas@35542]$ trace com.example.UserService * '#cost > 100'
# Filter by parameter values
[arthas@35542]$ watch com.example.UserService getUserById 'params[0] > 1000'
# Monitor multiple methods
[arthas@35542]$ monitor -c 10 com.example.UserService *
# Export results to file
[arthas@35542]$ trace com.example.UserService getUserById > /tmp/trace.log
Arthas is a powerful Java diagnostic tool that can help developers:
Key Points:
Arthas is an essential tool for Java developers, especially valuable for production environment troubleshooting and performance optimization.