Identify Slow Java Code Instantly with Arthas – No Manual Timers Needed

This guide shows how to use the open‑source Java diagnostic tool Arthas to attach to a running JVM, view real‑time metrics, and trace method execution without inserting manual timing code, enabling quick pinpointing and optimization of performance bottlenecks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Identify Slow Java Code Instantly with Arthas – No Manual Timers Needed

When an API’s response time suddenly spikes after the business logic is completed, developers often struggle to locate the slow code block because the code is complex and layered.

Typical manual instrumentation with System.currentTimeMillis() or Spring’s StopWatch is intrusive, time‑consuming, and must be removed after debugging.

Arthas Introduction

Arthas

is an Alibaba‑open‑source Java diagnostic tool that can attach to a JVM without restarting it, providing real‑time insight into load, memory, GC, threads, method parameters, return values, and execution time.

Arthas Quick Start

Download the Arthas jar and start it with the Java command:

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

Arthas lists the running Java processes; select the target process (e.g., 12512) and press Enter. It attaches to the process and prints connection logs.

Use the dashboard command to view a live panel showing metrics such as memory usage and GC status.

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

Using Trace Command to Measure Method Time

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

Example 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 application and run:

trace com.huangxy.springstudy.controller.HelloController test

The trace output shows that the two() method consumes 1029 ms (≈90.73% of total time).

Note that trace only follows sub‑calls of the matched method. To trace multiple methods, use a regular expression:

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

This reveals that the actual slow method is three(), allowing targeted optimization.

By leveraging Arthas’s non‑intrusive tracing capabilities, developers can quickly locate and fix performance bottlenecks in Java backend services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Spring BootArthasPerformance debuggingJava profilingtrace command
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.