arthas排查jvm中CPU占用過高問題解決
安裝 小試
記一次使用arthas排查jvm中CPU占用過高問題。這工具屌爆了 碾壓我目前使用的全部JVM工具。
curl -O https://arthas.aliyun.com/arthas-boot.jar java -jar arthas-boot.jar --repo-mirror aliyun --use-http
jar后面的參數(shù)也可以不加 加上只是為了下載速度更快
接下來arthas 控制臺中顯示了當前機器上jvm進程列表 輸入需要排查的jvm進程號即可進入監(jiān)控命令模式
找出CPU的元兇
處理問題之前 先想想如何去找到問題的原因 這個是解決問題個人覺得最重要的一步。
當前的現(xiàn)狀是jvm啟動后 cpu直接飆升到80+%。而內(nèi)存是正常的,可以認為大概率是某個線程占用了計算資源 導致的。所以第一步需要先把占用過高線程給揪出來。
這次使用arthas排查。也順便提一下以前記錄過用top -Hp的方法找出占用資源的線程PID 方法 top -Hp方法參考 。
輸入命令 thread 查看所有線程信息 默認是按照cpu資源占用排名的

可以看到當前線程lettuce-nioEventLoop-4-1 占用cpu高達47.75。其實這個線程名稱已經(jīng)能定位到具體某個方向的問題了,所以線程名稱的定義需要有意義 為了方便排查問題。
可以看出因為我們程序使用了lettuce做redis的客戶端,主要是使用了redis stream
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, String>> containerOptions =
StreamMessageListenerContainer.StreamMessageListenerContainerOptions.builder()
.batchSize(10) // 一次性最多拉取多少條消息
.targetType(String.class) // 目標類型。統(tǒng)一使用 String
.executor(mqConsumerExecutor)
.pollTimeout(Duration.ZERO)//0不超時
.build();把.pollTimeout(Duration.ZERO)這一句改為 .pollTimeout(Duration.ofMillis(10))cpu就正常了。
原因就是設置了永不超時 資源得不到釋放。
改為指定時間超時后 程序一點問題都沒有了。
查看線程棧的參數(shù)
可以直接使用thread pid 上圖占用最高的id為22 則輸入 thread 22 能看到類似jstack的功能
"lettuce-nioEventLoop-4-1" Id=22 RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:101)還有一個更好用的命令 -n參數(shù)能顯示top-n-threads 比上面一種更詳細
"lettuce-nioEventLoop-4-1" Id=22 cpuUsage=49.51% deltaTime=99ms time=392976ms RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:101)可以看出使用arthas排除這類問題 比使用top -Hp方便太多。當然這只是它的一個小功能而已。
以上就是arthas排查jvm中CPU占用過高問題解決的詳細內(nèi)容,更多關(guān)于arthas排查jvm CPU過高的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
@RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)
這篇文章主要介紹了@RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
JavaWeb使用Session和Cookie實現(xiàn)登錄認證
本篇文章主要介紹了JavaWeb使用Session和Cookie實現(xiàn)登錄認證,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
詳解Java異常處理中throw與throws關(guān)鍵字的用法區(qū)別
這篇文章主要介紹了詳解Java異常處理中throw與throws關(guān)鍵字的用法區(qū)別,這也是Java面試題目中的???需要的朋友可以參考下2015-11-11

