How to Pinpoint Java Performance Bottlenecks with Arthas
This article walks you through using the open‑source Java diagnostic tool Arthas to locate and analyze performance problems in Java applications, covering basic and advanced tracing, filtering, and flame‑graph visualization without modifying source code.
Arthas是什么
Arthas是阿里巴巴开源的 Java 诊断工具,能够在线诊断 Java 应用的性能问题,实时查看线程、类、对象、方法等信息,支持热更新、JVM 命令执行以及系统指标监控,适用于 Spring Cloud、Dubbo 等场景。
实时查看应用的线程、类、对象、方法等信息,支持热更新类和方法。
执行 JVM 命令,监控堆内存、GC、线程等系统指标。
支持 Spring Cloud、Dubbo 等分布式服务的诊断与治理。
实时监控请求响应时间并打印耗时最长的方法调用链。
追踪方法入参和返回值,支持 MVEL 表达式。
支持 Java 8 lambda 表达式的调试。
支持 Java 8 lambda 调试和诊断。
如何使用Arthas诊断代码性能问题
step1 启动应用。
step2 使用 Arthas 连接到 JVM 进程: java -jar arthas-boot.jar 选择对应的进程编号进入 Arthas 控制台。
step3 使用 trace {全限定类名} {方法名} 命令追踪方法耗时。
示例:在 controller 的 callServices 方法上执行 trace,可直接看到最耗时的调用链。
step4 根据输出定位耗时部分,继续对感兴趣的类和方法进行 trace,以逐层深入分析。
进阶诊断
可以根据入参或耗时进行过滤:
使用 OGNL 表达式过滤入参,例如仅当第一个参数等于 "abc" 时采集。
使用 #cost 关键字过滤耗时,例如只关注超过 180ms 的调用。
火焰图
Arthas 还能采样生成火焰图,帮助直观定位性能瓶颈。使用 profiler start 开始采样, profiler stop 停止并生成火焰图。
火焰图的纵轴表示调用栈,横轴表示方法执行时间,宽阔的峰顶通常指向性能瓶颈,例如示例中的 ServiceC.process (计算 1000 万以内所有质数)导致的高耗时。
通过上述步骤,开发者可以在不侵入代码的情况下快速定位并分析 Java 程序的性能问题。
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
