How to Pinpoint the Exact Java Code Causing 100% CPU Usage with jstack and Arthas

This guide explains why Java applications can hit 100% CPU in production, introduces the two primary diagnostic tools jstack and Arthas, and provides step‑by‑step commands and code examples to locate the exact source line responsible for the CPU spike.

ITPUB
ITPUB
ITPUB
How to Pinpoint the Exact Java Code Causing 100% CPU Usage with jstack and Arthas

Why CPU 100% Happens and How to Diagnose

CPU spikes in production often stem from Java code that consumes all CPU. Being able to locate the exact line of code is a key skill for interviews and troubleshooting.

Two Main Tools

jstack – the JVM‑provided thread‑stack dump utility.

Arthas – an open‑source Java diagnostic tool that also provides thread and stack information.

Using jstack

First find the process ID (PID) of the Java program, then run top -Hp <pid> to see which thread uses the most CPU. Convert the thread’s decimal ID to hexadecimal with printf "%x\n" <tid>, then dump the stack with jstack -l <pid>. The output shows each thread, its state, and the source line (e.g., JVMCPU.java:16) that is consuming CPU.

Example command sequence:

top -Hp 32208
printf "%x
" 32230   # => 0x7de6
jstack -l 32208

In the stack dump, look for the line containing JVMCPU$CPUTask.run(JVMCPU.java:16). This points to the busy loop in the sample code.

Sample Java Program

package com.tuling.learnjuc.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class JVMCPU {
    private static ExecutorService service = Executors.newFixedThreadPool(5);
    private static Object lock = new Object();

    public static class CPUTask implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                long sum = 0L;
                while (true) {
                    sum += 1;
                }
            }
        }
    }

    public static void main(String[] args) {
        CPUTask cpuTask = new CPUTask();
        service.execute(cpuTask);
    }
}

Using Arthas

Download and start Arthas:

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

In the Arthas console run dashboard to list top‑N threads, then thread -n 1 to print the stack of the busiest thread. The result is similar to jstack and also points to JVMCPU.java:16.

Both tools ultimately reveal that the infinite loop in while(true){ sum+=1; } is the cause of the CPU 100% usage.

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.

JavaLinuxArthasCPU profilingPerformance debuggingjstack
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.