Master Java Debugging with Arthas: Solve 8 Real‑World Problems
This article introduces Alibaba's open‑source Java diagnostic tool Arthas and demonstrates how to address eight common production issues—including locating class JARs, verifying code changes, online debugging without redeployment, monitoring JVM health, generating flame graphs, and finding class instances—through practical command‑line examples and code snippets.
Introduction
Arthasis Alibaba's open‑source Java diagnostic tool. It can help solve common problems such as locating the JAR a class is loaded from, checking why code changes are not taking effect, debugging without logs, monitoring JVM status, generating flame graphs, and finding class instances.
Problem List
Which JAR loads a class? Why class‑related exceptions?
Why code changes are not taking effect?
How to debug online without redeploying?
How to handle data issues that cannot be reproduced locally?
Is there a global view of system health?
How to monitor JVM real‑time status?
How to locate hotspots and generate flame graphs?
How to find instances of a class inside the JVM?
Solution Overview
The author provides detailed solutions using Arthas commands.
Preparation
Test Code
package com.shockang.study;
import com.alibaba.fastjson.JSON;
import lombok.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ArthasDemo {
public static void main(String[] args) {
String s = "[{\"name\":\"zhangsan\",\"age\":\"10\",\"telephone\":\"123456\",\"interests\":[\"sing\",\"dance\",\"rap\"]},
" +
"{\"name\":\"lisi\",\"age\":\"20\",\"telephone\":\"123457\",\"interests\":[\"sing\",\"swim\"]},
" +
"{\"name\":\"wangwu\",\"age\":\"30\",\"telephone\":\"123458\",\"interests\":[\"sing\",\"program\"]}]";
while (true) {
System.out.println(new ArthasDemo().convert(s));
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private List<People> convert(String s) {
return JSON.parseArray(s, People.class);
}
@Getter @Setter @ToString @FieldDefaults(level = AccessLevel.PRIVATE)
private static class People {
String name;
String age;
String telephone;
List<String> interests;
}
}Console Output
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/bin/java ...
[ArthasDemo.People(name=zhangsan, age=10, telephone=123456, interests=[sing, dance, rap]), ArthasDemo.People(name=lisi, age=20, telephone=123457, interests=[sing, swim]), ArthasDemo.People(name=wangwu, age=30, telephone=123458, interests=[sing, program])]Download and Run Arthas
Attach to a Java process and open the WebConsole at http://127.0.0.1:3658/.
Problem 1: Locate Class Jar
Use sc -d to find the classloader hash, then
classloader -c <hash> -r com/shockang/study/ArthasDemo.classto see the JAR path.
sc -d *ArthasDemo*
classloader -c 18b4aac2 -r com/shockang/study/ArthasDemo.class
file:/Users/.../target/classes/com/shockang/study/ArthasDemo.classProblem 2: Method Not Executed
Use watch and tt to observe method calls, e.g.:
watch com.shockang.study.ArthasDemo convert "{params,target,returnObj}" -f -x 4
tt -t com.shockang.study.ArthasDemo convertProblem 3: Online Debug Without Redeploy
Use redefine to hot‑replace method bodies, then observe the new behavior without restarting.
Problem 4: Unreproducible Data Issue
Combine tt with tee to dump command output to a file for later analysis.
tt -t com.shockang.study.ArthasDemo convert | tee /Users/shockang/Downloads/logProblem 5: Global System View
Run dashboard to see real‑time metrics such as QPS, latency, thread‑pool status, etc.
Problem 6: JVM Real‑Time Status
Use jvm command to view CPU, memory, GC, and thread information.
Problem 7: Generate Flame Graph
Run profiler (default CPU) and open the generated files under http://localhost:3658/arthas-output/.
Problem 8: Find Class Instances
Use
vmtool --action getInstances --className java.lang.String --limit 10to list instances, optionally specifying classloader hash.
vmtool --action getInstances --className java.lang.String --limit 10
@String[][ @String[com/taobao/arthas/core/shell/session/Session], ... ]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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
