Java 线程卡死排查 — 命令备忘(Windows + CentOS)

Java 线程卡死排查 — 命令备忘(Windows + CentOS)


1. 找 Java 进程 PID

:: Windows
jps -l
tasklist | findstr /I "java"
# CentOS
jps -l
ps -ef | grep java | grep -v grep

2. 抓线程栈(间隔5秒抓两份)

:: Windows
jstack <PID> > C:\temp\dump1.txt 2>&1
timeout /t 5
jstack <PID> > C:\temp\dump2.txt 2>&1
# CentOS
jstack <PID> > /tmp/dump1.txt 2>&1
sleep 5
jstack <PID> > /tmp/dump2.txt 2>&1

3. 搜关键栈帧

:: Windows CMD
findstr /C:"socketRead0" C:\temp\dump1.txt
findstr /C:"yourMethod" C:\temp\dump1.txt

:: Windows PowerShell(带上下文)
Select-String -Path C:\temp\dump1.txt -Pattern "socketRead0" -Context 3,3
# CentOS(-C 显示前后各3行)
grep -C 3 "socketRead0" /tmp/dump1.txt
grep -C 3 "yourMethod" /tmp/dump1.txt

4. 查网络连接

:: Windows
netstat -ano | findstr ":21"
netstat -ano | findstr ":80"
netstat -ano | findstr "ESTABLISHED" | findstr "<PID>"
# CentOS
netstat -antp | grep ":21"
netstat -antp | grep ":80"
netstat -antp | grep "<PID>" | grep ESTABLISHED
# 或用 ss(更快)
ss -antp | grep ":21"
ss -antp | grep "<PID>"

5. 测对方端口是否通

:: Windows PowerShell
Test-NetConnection -ComputerName 192.168.1.100 -Port 80
# CentOS
nc -zv 192.168.1.100 80 -w 5
curl -m 5 -s -o /dev/null -w "%{http_code}" http://192.168.1.100/
telnet 192.168.1.100 80          # 通了会黑屏,Ctrl+] 退出

6. 查文件被谁锁着

:: Windows 图形界面
resmon    → CPU标签 → 关联的句柄 → 搜文件名

:: Windows 命令
handle D:\receivefile\c572d91a      :: 需装 Sysinternals Handle
# CentOS
lsof | grep "c572d91a"
lsof /receivefile/c572d91a-xxx.rar
fuser -v /receivefile/c572d91a-xxx.rar

7. 杀进程释放锁

:: Windows
taskkill /F /PID <PID>
# CentOS
kill -9 <PID>
# 批量杀同类进程
pkill -9 -f "关键字"

8. 实时跟踪日志

:: Windows PowerShell(相当于 tail -f)
Get-Content "D:\app.log" -Wait -Tail 50
# CentOS
tail -f /var/log/app/app.log
tail -50f /var/log/app/app.log            # 先看最后50行再跟踪

9. 查 CPU / 内存异常(辅助判断)

:: Windows
taskmgr                                          :: 图形界面
wmic process where "name='java.exe'" get ProcessId,WorkingSetSize
# CentOS
top -Hp <PID>                                    # 看该进程下每个线程的 CPU
jstat -gcutil <PID> 1000 5                       # 每秒看一次 GC,共5次

速查对照表

目的 Windows CentOS
找 PID jps -l jps -l / ps -ef | grep java
抓线程栈 jstack <PID> > x.txt 2>&1 jstack <PID> > x.txt 2>&1
搜栈帧 findstr /C:"key" file grep -C 3 "key" file
查连接 netstat -ano | findstr ":端口" ss -antp | grep ":端口"
测端口 Test-NetConnection ip -Port xx nc -zv ip port -w 5
查文件锁 resmon 图形界面 lsof | grep 文件名
杀进程 taskkill /F /PID xxx kill -9 xxx
跟踪日志 Get-Content file -Wait tail -f file
查线程CPU 无原生命令 top -Hp <PID>
查GC 无原生命令 jstat -gcutil <PID> 1000

posted on 2026-09-01 10:08  何苦->  阅读(8)  评论(0)    收藏  举报

导航