Master Arthas: Zero-Downtime Java Debugging, Live Logs & Hot Code Reload
This guide demonstrates how to use Arthas, Alibaba's open-source Java diagnostic tool, to troubleshoot production issues without downtime—covering installation, core commands (dashboard, trace, watch, stack, logger, redefine), three real-world case studies, and critical production safety rules.
Why Arthas? The Blind Spot of JDK Native Tools
JDK built-in tools (jps, jstat, jstack, jmap) excel at JVM resource issues—CPU spikes, OOM, GC anomalies, deadlocks. However, they only show results, not the execution process. In production you often face:
Interface timeouts with no latency logs—unable to pinpoint which line or method is slow.
Intermittent bugs unreproducible locally; adding logs requires code change, rebuild, redeploy—slow and risky.
Third-party or utility classes throwing exceptions without logs.
Minor bugs where restart cost is too high; need a hot fix without downtime.
Arthas, Alibaba's open-source Java diagnostic tool, fills this gap. It uses attach-based mounting , requires no code changes, no restart, and imposes minimal overhead—safe for production.
Arthas vs. Traditional Troubleshooting
Traditional: missing logs → code change → package → deploy → restart (10+ minutes, impacts stability).
Arthas: command-line attach, real-time monitoring of method params, return values, latency, exceptions—second-level diagnosis, zero downtime, zero deployment.
Arthas does not replace JDK tools; it complements them: JDK tools for JVM resource issues, Arthas for business-code execution details.
Production Quick Install & Startup
1. One-Click Launch Script (Production Standard)
# One-click download and start arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar2. Attach Target Process
After launch, all Java processes are listed; enter the index to attach to the target service.
3. Exit Rules (Critical for Production)
quit: exit client, keep Arthas attached—monitoring remains active. stop: fully stop Arthas, release attached resources. Must run after diagnosis to avoid long-term resource occupation.
High-Frequency Core Commands (Cover 99% of Production Scenarios)
1. dashboard: Full-Service Monitoring Panel
Run dashboard immediately after attach to see thread states, CPU, memory, GC, active/blocked threads at a glance. Value: single view to judge overall health—thread, memory, or CPU issue.
2. trace: Method Latency Chain Tracing (Core Tool for Slow/Timeout Interfaces)
Scenario: interface timeout, no latency logs, unknown which sub-method or code segment is slow. trace prints per-step latency inside a method, pinpointing slow code, slow SQL, slow external calls.
# Format: trace fully-qualified-class method-name
trace com.xxx.UserService loginProduction Tips:
Precisely locate loop latency, I/O latency, Redis/MQ/DB call latency.
Auto-filters fast executions, highlights high-latency paths.
No instrumentation, no code changes—real-time execution chain capture.
3. watch: Dynamic Monitoring of Params, Return Values, Exceptions
Scenario: intermittent errors, data anomalies, unreproducible locally, missing input logs. watch is the most used command; it monitors method invocations in real time: input params, return values, thrown exceptions, invocation count.
# Monitor params, returnObj, throwExp, print in real time
watch com.xxx.UserService login "{params,returnObj,throwExp}" -n 10 -x 3Parameter Breakdown: params: method input parameters. returnObj: method return value. throwExp: exception information. -n 10: limit to 10 invocations to avoid log flooding. -x 3: object expansion depth for complex parameter inspection.
Core Value: Whatever logs are missing in production, add them in real time —no release, no restart.
4. stack: Full Call Stack of a Method
Scenario: method executes unexpectedly, repeatedly, or is called from unknown source. stack prints the complete call stack, precisely locating the caller—solves mystery executions and duplicate triggers.
stack com.xxx.UserService login5. logger: Dynamic Log Level Adjustment (Live Tuning Tool)
Scenario: production runs at INFO; need temporary DEBUG for details without restart.
Arthas supports dynamic temporary log level changes , effective immediately; revert after diagnosis.
# View current log levels
logger
# Temporarily set global level to DEBUG
logger --level DEBUG6. redefine: Hot Code Reload (Fix Minor Bugs Without Restart)
Scenario: non-critical bug in production; restart cost high; need quick temporary fix.
Arthas supports redefine to hot-load a .class file, replacing running code without restart.
Strict Production Rules:
Only for emergency temporary fixes; never rely on hot updates long-term .
Hot update cannot add methods or fields—only modify existing logic.
After fix, must sync to formal version to ensure code consistency.
Three Classic Production Fault Case Studies
Case 1: Intermittent Interface Timeout, No Errors, No Latency Logs
Traditional: clueless—guess DB or Redis.
Arthas Flow:
Use trace on the timeout interface to capture full execution chain.
Pinpoint a third-party HTTP call lacking timeout control, occasionally blocking.
Root cause identified; add timeout and retry—intermittent timeouts eliminated.
Case 2: Production Data Anomaly, Unreproducible Locally
Traditional: cannot get real production inputs; blind code tweaks.
Arthas Flow:
Use watch to capture real production input params and return values.
Discover special parameter scenario not covered in local tests.
Reproduce bug with real production data; precise fix applied.
Case 3: Unknown Scheduled Task Consuming CPU
Traditional: scan code for scheduled tasks—time-consuming, source not found.
Arthas Flow: dashboard reveals scheduled threads frequently consuming CPU. stack traces the task execution chain.
Locate an abandoned, unclosed scheduled task; comment out to fix.
Production Taboos & Pitfall Checklist (Must Follow)
No high-frequency monitoring during peak hours: trace, watch high-frequency printing slightly degrades performance—use only during incidents, never leave attached daily.
Must run stop after diagnosis: avoid long-term attachment occupying JVM resources.
Hot updates only for emergencies: never substitute version releases; prevents code version chaos.
Never monitor core high-throughput interfaces: flash-sale, payment, high-QPS endpoints—avoid log flooding and performance impact.
Summary
JDK native commands solve JVM-level resource failures ; Arthas solves business-code-level execution failures .
Master the six core commands— dashboard, trace, watch, stack, logger, redefine —and you gain the ability to diagnose 90% of elusive production faults, completely abandoning the inefficient "code change, restart, trial-and-error" approach.
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.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
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.
