Speed Up Java Debugging: One‑Click Tools to Pinpoint 100% CPU and Deadlocks
Backend developers often face sudden high CPU usage or thread deadlocks in production Java services, and this article introduces a set of one‑click scripts and practical examples that automate diagnosis, locate offending code lines instantly, and provide additional utilities for classpath conflicts, JVM monitoring, and runtime tracing.
Background
Backend developers often encounter sudden high CPU load on production servers, especially during off‑hours, and need a fast, reliable method to locate the offending code.
Traditional 4‑step manual process
1. top -o ... with P:1040 // sort by process load
2. top -Hp <pid> // find thread PID
3. printf "0x%x" <tid> // convert to hex for jstack
4. jstack <pid> | vim +/0x... // view stack traceThis procedure is too cumbersome for urgent incident response.
One‑click tool: show-busy-java-threads.sh
The script automates the above steps, instantly identifying the most CPU‑intensive Java threads and displaying their stack traces.
source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/master/test-cases/self-installer.sh)Example 1 – Regex backtracking causing 100% CPU
import java.util.*;
// ... Java code that matches complex regex patterns ...Running the program creates a Java process that consumes 100% CPU. The tool pinpoints the thread executing Pattern$GroupHead.match in the regex engine.
Example 2 – Thread deadlock leading to hang
public class SimpleDeadLock extends Thread {
static Object l1 = new Object();
static Object l2 = new Object();
// ... threads acquire locks in opposite order ...
}The script reveals the circular lock acquisition, showing the exact lines where each thread waits for the other's lock.
Additional useful scripts
show-duplicate-java-classes : Detect duplicate classes in JARs or class directories.
find-in-jars : Search for classes or resources inside JAR files using regex.
housemd : JVM diagnostic tool supporting class loading, method tracing, and more.
jvm : Interactive menu for thread, GC, heap, and other JVM metrics.
greys : Non‑intrusive Java runtime tracing (Linux/Unix/Mac only).
sjk : Suite of JVM performance utilities (ttop, jps, hh, gc, etc.).
These tools together form a practical toolbox for quickly diagnosing CPU spikes, deadlocks, classpath conflicts, and other runtime issues in Java backend services.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
