Java进程CPU占用率100%问题排查

100%指的是占用了CPU一个核心,两个核心是200%,以此类推。
CPU占用率及对应进程ID(pid)能够经过top命令肯定,在top界面按 c (显示完整的命令行参数),按 1 (显示每一个核心的统计数据)。java

这个问题最多见的有如下几种可能:oracle

一、堆内存不足致使频繁Full GC
能够经过两个命令肯定less

sudo jmap -heap pid 查看堆内存的消耗状况工具

sudo jstat -gc pid interval count 查看GC状况,示例:sudo jstat -gc 5746 3000 5 表明查看5746进程的GC状况、每隔3000毫秒打印一次、总共打印5次。若是FGC/FGCT增加明显,说明Full GC很频繁。命令行

后续处理:线程

  • 若是状况紧急,那得立刻重启Java应用进程
  • 不紧急的话须要获取相关信息用于分析为何堆内存被消耗完了,可能有内存泄漏问题,能够用 1)sudo jmap -histo pid | head -n 20 查看Java对象的占用统计信息,2)sudo jmap -dump:live,format=b,file=heap.bin pid 把堆转储导出到本地文件,能够用 Eclipse MAT 工具分析内存泄漏

二、代码实现问题
思路:追查具体是哪一个线程占用了CPU,1)先查到本地系统CPU占用率高的线程ID,2)找到对应的Java线程及线程堆栈code

top -H -p pid 查看某个进程里面哪些线程占用了CPU,把对应的线程ID拷贝下来,转为十六进制【IDEA》Tools》Groovy Console》println Long.toHexString(1234) 便可完成转换】。orm

sudo jstack -l -F pid | less 获取Java线程堆栈,用十六进制的本地线程ID搜索,会在某一行的nid处找到对应的线程。查看Java线程堆栈,找到对应的Java类及行号,而后阅读代码查找可能的问题缘由。htm

jstack堆栈信息里tid/nid的说明
https://docs.oracle.com/javas...对象

The thread dump consists of the thread stack, including the thread state, for all Java threads in the virtual machine.

The header line contains the following information about the thread:
- Thread ID (tid), which is the address of a thread structure in memory.
- ID of the native thread (nid).
相关文章
相关标签/搜索