Using Arthas to Quickly Diagnose Production Issues in Java Applications
This article demonstrates how the open‑source Java diagnostic tool Arthas can be employed in various production scenarios—such as CPU spikes, class‑loading conflicts, method‑level monitoring, performance bottlenecks, and hot‑code replacement—to rapidly locate and resolve runtime problems without restarting services.
Developers often worry about production incidents that can degrade user experience or cause financial loss; while pre‑deployment testing and post‑incident mitigation are out of scope, this guide focuses on fast problem localisation using the open‑source Java diagnostic tool Arthas.
Arthas is a non‑intrusive, easy‑to‑install Java debugging assistant. Its documentation is available at https://arthas.aliyun.com/doc . The article walks through five realistic scenarios, each illustrated with a small demo.
Scenario 1 – CPU Spike: A demo program continuously inserts data into a map, causing CPU usage to reach 300%. Using the command thread -n 3 Arthas instantly shows the offending thread and the exact source line.
static void testCPU() {
while (true) {
for (int i = 0; i < 10000; i++) {
HOLDER.put(i, i);
}
HOLDER.clear();
}
}The same approach identifies a dead‑lock situation with thread -b , revealing the waiting threads and the locked synchronizer.
[arthas@28004]$ thread -b
"Thread-0" Id=11 WAITING on java.util.concurrent.locks.ReentrantLock$NonfairSync@4bcf34d owned by "Thread-1" Id=12
at ...
Number of locked synchronizers = 1
- java.util.concurrent.locks.ReentrantLock$NonfairSync@6b008751 <---- but blocks 1 other threads!Scenario 2 – Class/Method Version Conflicts: When a deployed service cannot find a class or method, Arthas commands sc -d , sm , and jad help locate the loaded class, its JAR source, and decompiled bytecode.
[arthas@30314]$ sc com.ljy.test.util.DemoUtils
com.ljy.test.util.DemoUtils
Affect(row-cnt:1) cost in 6 ms.Regular expressions can be used for fuzzy searches, e.g., sc *DemoUtils or sc -d org.apache.commons.lang3.StringUtils to pinpoint the exact JAR.
Scenario 3 – Real‑time Method Observation: The watch command monitors method parameters, return values, and exceptions. Using OGNL expressions, developers can filter specific invocations, such as only when the first parameter equals "a".
[arthas@31318]$ watch *TroubleShootDemo invoke "{params,returnObj}" -x 2
Press Q or Ctrl+C to abort.
...Scenario 4 – Performance Bottleneck Identification: The trace command records method execution times. By applying an OGNL filter like #cost>10 , only methods exceeding 10 ms are displayed, highlighting the slowest call.
[arthas@1638]$ trace *ArthasDemo invoke
Press Q or Ctrl+C to abort.
`---[1079.223985ms] com.ljy.test.util.ArthasDemo:invoke()
+---[52.155584ms] com.ljy.test.util.ArthasDemo:methodB()
+---[1002.240458ms] com.ljy.test.util.ArthasDemo:methodC()
`---[21.386992ms] com.ljy.test.util.ArthasDemo:methodA()Scenario 5 – Hot Code Replacement: The redefine command swaps a running class with a new version without restarting the JVM, enabling emergency fixes during high‑traffic events.
public class RedefineDemo {
void method() {
System.out.println("before");
}
}
public class Test {
public static void main(String[] args) throws IOException {
RedefineDemo demo = new RedefineDemo();
demo.method();
System.in.read();
demo.method();
}
}After compiling a modified RedefineDemo and executing redefine /path/to/RedefineDemo.class , subsequent calls print "after", demonstrating live patching (though it is risky and should be a last resort).
In conclusion, Arthas provides a powerful toolbox for quickly locating CPU issues, class‑loading conflicts, method‑level bugs, performance hotspots, and even applying hot patches, greatly improving the efficiency of production troubleshooting for Java developers.
New Oriental Technology
Practical internet development experience, tech sharing, knowledge consolidation, and forward-thinking insights.
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.