How to Quickly Identify Slow Java Code Blocks with Arthas

This guide explains how to use the open‑source Java diagnostic tool Arthas to attach to a running Spring Boot application, view real‑time metrics, and trace method execution in order to pinpoint and optimize performance‑critical code sections without modifying the source.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
How to Quickly Identify Slow Java Code Blocks with Arthas

When an API implementation shows long response times in Postman, manually inserting timing code such as System.currentTimeMillis() or Spring's StopWatch can be tedious and intrusive.

public static void main(String[] args) {
    Long startTime = System.currentTimeMillis();
    exec();
    Long endTime = System.currentTimeMillis();
    log.info("exec method took {} ms", endTime - startTime);
}
public static void main(String[] args) throws InterruptedException {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start("exec");
    exec();
    stopWatch.stop();
    System.out.println(stopWatch.prettyPrint());
}

Both approaches require adding and later removing instrumentation code. Arthas provides a non‑intrusive alternative.

Arthas Overview

Arthas is an open‑source Java diagnostic tool from Alibaba that can inspect a running JVM without restarting it, showing class loading, memory, GC, thread status, method arguments, return values, and execution time.

Getting Started with Arthas

Download the Arthas boot jar and start it with java -jar arthas-boot.jar. The tool lists active Java processes; choose the target process number to attach.

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 12512 com.huangxy.springstudy.SpringStudyApplication
  [2]: 12511 org.jetbrains.jps.cmdline.Launcher

After attaching, the dashboard command displays a real‑time panel with metrics such as memory usage and GC status. dashboard The panel lists threads, memory consumption, and other useful statistics.

For a full list of commands, refer to the official Arthas documentation.

Using the Trace Command to Measure Method Duration

The trace command searches for a class‑pattern/method‑pattern and records the execution time of the matched call chain.

Consider the following Spring Boot 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 process and run:

trace com.huangxy.springstudy.controller.HelloController test

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

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

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

The extended trace reveals that three() is the actual slow method, allowing targeted optimization.

JavaSpringBootprofilingArthas
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

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.