Understanding jps and jstack: A Guide to Java Process Inspection and Thread Dump Analysis
This article introduces the jps tool for listing Java processes, explains its common options and usage examples, then provides a detailed overview of jstack, including its purpose, command syntax, thread‑state definitions, sample outputs, and practical tips for detecting deadlocks, counting threads, and diagnosing high CPU usage in Java applications.
Before introducing jstack, the article briefly explains jps , a simple command that lists Java processes for the current user.
It describes jps’s purpose as a process‑query tool, shows how to obtain Java process IDs using commands such as ps -ef | grep java , and notes that jps only searches processes owned by the current user.
Common jps options are presented with examples:
# 查看 java 进程,显示 pid,完整包名以及 main 函数参数
jps -ml
# 查看 Java 进程对应的给JVM的参数
jps -v
# 根据 name 查找 java 进程
jps -mlvV | grep
# 根据 name 查找 pid
jps | grepAn example command jps -lm and its output screenshots are shown, followed by a brief note that the jps section ends here and the article proceeds to jstack.
jstack Introduction
jstack generates a snapshot of the current threads in a Java Virtual Machine, showing the stack trace of each thread to help locate long pauses caused by deadlocks, infinite loops, or waiting on external resources.
The official documentation link is provided: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstack.html
Command syntax and options are listed:
Usage:
jstack [-l]
(to connect to running process)
jstack -F [-m] [-l]
(to connect to a hung process)
jstack [-m] [-l]
(to connect to a core file)
jstack [-m] [-l] [server_id@]
(to connect to a remote debug server)
Options:
-F force a thread dump when the process does not respond
-m print both Java and native frames (mixed mode)
-l long listing, includes lock information
-h or -help display help messageBefore using jstack, the article reminds readers to understand thread states, which are enumerated as:
NEW – not started, does not appear in a dump
RUNNABLE – ready to execute in the JVM
BLOCKED – waiting for a monitor lock
WAITING – waiting indefinitely for another thread
TIMED_WAITING – waiting with a timeout
TERMINATED – thread has exited
jstack Output Example
Typical usage to dump a thread stack is shown:
# 输出结果
jstack -l pid
# 输出结果到日志中
jstack -l pid > /tmp/jstack.logA sample excerpt of the full thread dump is included, illustrating thread names, IDs, states (RUNNABLE, WAITING), and locked synchronizers.
Practical Tips
The article provides three useful techniques:
Detecting deadlocks: Use jstack to find Java‑level deadlocks, e.g., the output contains “Found one Java‑level deadlock”.
Counting threads: Pipe jstack output to grep 'java.lang.Thread.State' | wc -l to count active threads.
Diagnosing high CPU usage: Identify the high‑CPU process, locate the offending thread ID, convert it to a Java thread ID, and analyze the stack trace; a linked article demonstrates the full workflow.
Finally, the article includes several promotional links and a “tips” section that mentions a collection of Java interview materials, but the core instructional content remains focused on jps and jstack usage for Java process debugging.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.