JVM实战学习——排查java程序 磁盘IO占用过高、CPU占用过高、内存占用过高(pidstat)
一、排查cpu高占用
1.示例代码
2.查询项目进程
1)使用 jps 命令查询项目启动的进程
[root]# jps -l
22640 study-java-jvm-app-1.0-SNAPSHOT.jar ## 其中 22640 就是项目进程
1349 -- process information unavailable
22714 sun.tools.jps.Jps
2)或者使用 ps -ef | grep java 同样可以查询进程号
[root]# ps -ef | grep java
## 其中 22640 就是项目进程
root 22640 17949 9 09:57 pts/0 00:00:06 java -jar study-java-jvm-app-1.0-SNAPSHOT.jar
root 22871 21072 0 09:58 pts/1 00:00:00 grep java
3.使用 jstack 工具导出堆栈信息,并进行死锁检测
## -l 死锁检测,输出找到的死锁信息
## 22640 即进程号
## > jstack.text 将堆栈信息导出到文件
[root]# jstack -l 22640 > jstack.text
## 死锁的堆栈信息如下所示:
## 根据堆栈信息可以找到对应的代码
## at c_6_3_6.DeadLock$CatThread.run(DeadLock.java:56)
## at c_6_3_6.DeadLock$DogThread.run(DeadLock.java:31)
Found one Java-level deadlock:
=============================
"Thread-3":
waiting for ownable synchronizer 0x00000000e31412e0, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-2"
"Thread-2":
waiting for ownable synchronizer 0x00000000e31412b0, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-3"
Java stack information for the threads listed above:
===================================================
"Thread-3":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000e31412e0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
at c_6_3_6.DeadLock$CatThread.run(DeadLock.java:56)
"Thread-2":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000e31412b0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
at c_6_3_6.DeadLock$DogThread.run(DeadLock.java:31)
Found 1 deadlock.