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).
相關文章
相關標籤/搜索