java程序cpu飙高的排查

92 阅读1分钟

java程序cpu飙高的排查

​ 针对于线上如果cpu过高的排查方法

  • 首先使用top命令:

    root@92346f121e14:/app# top
    

    image-20240923171457317

可以看到pid为1的这个进程cpu占有比较高

  • 使用top -Hp命令查看哪个线程占用cpu过高

    root@92346f121e14:/app# top -Hp 1
    

    image-20240923171812005

    可以看到线程id为24的cpu占有最高

  • 将线程id转为16进制

    root@92346f121e14:/app# printf "0x%x" 24
    0x18 
    

    返回的16进制是0x16

  • jstack工具跟踪堆栈定位代码

    root@92346f121e14:/app# jstack 1 | grep 0x18 -A 10
    "http-nio-8080-exec-1" #16 daemon prio=5 os_prio=0 tid=0x00007f6c38961800 nid=0x18 runnable [0x00007f6c21416000]
       java.lang.Thread.State: RUNNABLE
    	at org.example.controller.CpuCheckController.lambda$check$0(CpuCheckController.java:20)
    	at org.example.controller.CpuCheckController$$Lambda$539/408842102.run(Unknown Source)
    	at java.lang.Thread.run(Thread.java:750)
    	at org.example.controller.CpuCheckController.check(CpuCheckController.java:24)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
    
    

    可以定位到CpuCheckController第20行代码

    查看项目的代码,果然是死循环了:

    image-20240923172616182