Backend Development 8 min read

Using Arthas to Locate Slow Code Blocks in Java Applications

This article explains how to use the open‑source Java diagnostic tool Arthas to attach to a running JVM, view real‑time metrics, and employ the trace command to pinpoint and analyze time‑consuming methods without inserting manual timing code.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Using Arthas to Locate Slow Code Blocks in Java Applications

When a newly implemented API shows a long response time in Postman, developers often struggle to locate the slow code segment because the business logic is complex and layered.

Many resort to manual instrumentation with System.currentTimeMillis() or Spring's StopWatch , which is intrusive and cumbersome to clean up after debugging.

The article introduces Arthas , an Alibaba‑open‑source Java diagnostic tool that can attach to a JVM without restart and provide real‑time insights such as class loading, memory, GC, thread status, method arguments, return values, and execution time.

Quick start : download the arthas-boot.jar and launch it with java -jar arthas-boot.jar . The tool lists running Java processes; select the target process number to attach.

$ curl -O https://arthas.aliyun.com/arthas-boot.jar
$ java -jar arthas-boot.jar

After attachment, the dashboard command displays a live panel with metrics like memory usage and GC statistics.

$ dashboard
ID   NAME                GROUP   PRIORITY STATE    %CPU DELTA_TIME TIME   INTERRUPT DAEMON
36   DestroyJavaVM        main    5        RUNNABLE 0.0  0.000      0:1.748 false    false
...

The trace command can search for a class‑pattern / method‑pattern and render the full call chain with timing information.

Example controller:

@RestController
public class HelloController {
    @GetMapping("/test")
    public String test() throws InterruptedException {
        one();
        two();
        return "hello";
    }
    private void two() throws InterruptedException { Thread.sleep(20); three(); }
    private void three() throws InterruptedException { Thread.sleep(1000); }
    private void one() throws InterruptedException { Thread.sleep(100); }
}

Attach Arthas to the Spring Boot application and run:

$ trace com.huangxy.springstudy.controller.HelloController test

The trace output shows the call stack and time spent in each method, revealing that two() consumes about 1029 ms (≈90.73% of total).

To trace deeper levels, use a regular‑expression pattern:

$ trace -E com.huangxy.springstudy.controller.HelloController test|two

This highlights that the actual bottleneck is the three() method, allowing targeted optimization.

Overall, Arthas provides a non‑intrusive, powerful way to diagnose performance issues in backend Java services.

backenddebuggingjavaPerformance ProfilingArthasTrace
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.