Simple Guide to Using Arthas

3 min
AI 总结
$
|
Article Summary Image
Article Summary Image

Introduction to Arthas

Arthas is a powerful Java diagnostic tool open-sourced by Alibaba in September 2018. It supports JDK 6 and above, provides an interactive command-line interface, supports multiple platforms (Linux/Mac/Windows), and features Tab auto-completion. Arthas enables developers to quickly diagnose and locate production issues in Java applications without modifying code or restarting services.

Why Do We Need Arthas?

In daily development, traditional methods are inefficient when encountering the following Java issues:

  • High CPU load, but unable to locate specific threads
  • Thread deadlocks causing system freeze, difficult to view thread status in real-time
  • Slow application response, unclear method call chains
  • Production code inconsistent with expectations, code not taking effect after hot deployment
  • Remote debugging prohibited in production environments, unable to view variables in real-time
  • Lack of real-time JVM status and performance monitoring ::

Due to these pain points, Arthas was created to help developers achieve problem location, real-time monitoring, and code hot-swapping.

Installing and Starting Arthas

Installation

The simplest method is to download the official arthas-boot.jar:

::tab{

=’[“GitHub”, “Gitee”]’} #tab1

# Download from GitHub
curl -O https://arthas.aliyun.com/arthas-boot.jar

#tab2

# Recommended for users in China - download from Gitee
curl -O https://arthas.gitee.io/arthas-boot.jar

::

Starting

Start using the java -jar command:

::tab{

=’[“Interactive Mode”, “Specify PID”]’} #tab1

# Start Arthas and select Java process
java -jar arthas-boot.jar

#tab2

# Or directly specify process PID
java -jar arthas-boot.jar [PID]

::

After running, it will list Java processes on the system. Select the process number you need to diagnose.

Common Arthas Commands

Arthas has a rich set of commands, which can be categorized as follows:

::tab{

=’[“Basic Commands”, “Monitoring Commands”, “Class Operations”, “Enhancement Commands”]’} #tab1

CommandDescription
helpView command help information
catPrint file content
grepSearch for matching results
pwdReturn current working directory
clsClear current screen
sessionView current session information
resetReset class enhancement state
versionOutput current Arthas version
quitExit current client
shutdownShutdown Arthas server

#tab2

CommandDescription
dashboardDisplay current system real-time data panel
threadView JVM thread stack information
jvmView JVM information
syspropView and modify JVM system properties
sysenvView JVM environment variables
vmoptionView and modify JVM diagnostic options
perfcounterView JVM performance counter information
loggerView and modify logging levels

#tab3

CommandDescription
scView loaded class information
smView class method information
jadDecompile loaded class source code
mcCompile Java file to Class file in memory
redefineRedefine loaded classes
dumpExport bytecode of loaded classes
classloaderView classloader hierarchy, paths, etc.

#tab4

CommandDescription
monitorMethod execution monitoring
watchMonitor method input parameters and return values
traceTrace method call path and time consumption
stackOutput current method call path
ttTime tunnel for recording method call parameters and return values
::

Practical Use Cases

Case 1: Locate Threads with High CPU Usage

Locate specific threads and stacks consuming resources.

Case 2: Troubleshoot Slow Application Response

Use the trace command to trace interface method calls and find the most time-consuming methods.

Case 3: View Class Loading Information

sc -d ClassName views detailed loading information for a specified class, including class loading path and classloader, helping troubleshoot class loading exceptions.

Case 4: Detect and Fix Thread Deadlocks

Locate threads waiting for each other’s locks.

Case 5: Observe Method Input Parameters and Return Values

View method call parameters and return values in real-time, facilitating debugging and understanding business behavior.

Arthas Web Console

Arthas provides a graphical Web Console. After starting Arthas, visit: http://127.0.0.1:8563/

The Web Console has the same functionality as the command line, supports command history and real-time feedback, facilitating team collaboration and troubleshooting.

Important Notes

  • Arthas is a diagnostic tool. Use with caution in production environments, requiring approval. Avoid operations during peak hours.
  • Some commands (such as trace, watch, tt) modify bytecode and may affect performance. Long-term running may bring memory leak risks.
  • Enhanced classes will be automatically reset when Arthas exits. You can manually execute the reset command.
  • Strict permission control and operation logging should be implemented when using in production environments. ::

Summary

Through this article, you have mastered the basics and practical applications of Arthas, enabling you to efficiently troubleshoot Java production issues and improve tuning capabilities. It is recommended to practice more in projects to gradually master this powerful tool.


::


::


::


::