Linux下java进程CPU占用率高分析方法
程序员文章站
2022-05-17 16:15:54
在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况。这种情况发生时,我们怎么去找出原因并解决。一般解决方法是通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用。 转载自:https://blog.linuxeye.cn/343.html 1. 通过to ......
在工作当中,肯定会遇到由代码所导致的高cpu耗用以及内存溢出的情况。这种情况发生时,我们怎么去找出原因并解决。
一般解决方法是通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用。
转载自:https://blog.linuxeye.cn/343.html
1. 通过top命令找到可疑进程pid
1 top - 21:53:42 up 157 days, 4:09, 1 user, load average: 2.41, 2.61, 2.66 2 tasks: 102 total, 1 running, 101 sleeping, 0 stopped, 0 zombie 3 %cpu(s): 38.8 us, 5.4 sy, 0.0 ni, 52.4 id, 0.0 wa, 0.0 hi, 3.4 si, 0.0 st 4 kib mem : 16267724 total, 349784 free, 1985360 used, 13932580 buff/cache 5 kib swap: 0 total, 0 free, 0 used. 13919376 avail mem 6 7 pid user pr ni virt res shr s %cpu %mem time+ command 8 13156 admin 20 0 8134276 1.176g 22068 s 191.3 7.6 25500:51 java 9 1564 root 20 0 2510388 35392 4076 s 1.3 0.2 2645:01 staragent-core 10 26913 root 0 -20 129104 13524 9372 s 0.7 0.1 49:07.18 aliyundun 11 13 root 20 0 0 0 0 s 0.3 0.0 767:46.01 ksoftirqd/1 12 23 root 20 0 0 0 0 s 0.3 0.0 749:58.78 ksoftirqd/3 13 357 root 20 0 123600 53312 52884 s 0.3 0.3 47:45.46 systemd-journal 14 1566 root 20 0 804172 5796 2080 s 0.3 0.0 504:56.46 staragent-ppf 15 27517 root 20 0 146016 5420 4064 s 0.3 0.0 0:00.01 sshd 16 27551 root 20 0 157584 2116 1500 r 0.3 0.0 0:00.01 top 17 1 root 20 0 125324 3296 2016 s 0.0 0.0 23:18.56 systemd 18 2 root 20 0 0 0 0 s 0.0 0.0 0:02.70 kthreadd 19 3 root 20 0 0 0 0 s 0.0 0.0 51:35.55 ksoftirqd/0 20 5 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 kworker/0:0h 21 7 root rt 0 0 0 0 s 0.0 0.0 1:12.22 migration/0 22 8 root 20 0 0 0 0 s 0.0 0.0 0:00.00 rcu_bh 23 9 root 20 0 0 0 0 s 0.0 0.0 56:09.94 rcu_sched 24 10 root rt 0 0 0 0 s 0.0 0.0 0:36.01 watchdog/0
从上面命令中可以看出java进程cpu利用率一直保持200%左右,稳居不下,找到pid 13156
2. 找出消耗资源最高的线程
top -h -p 24138 可以不用第一步,直接执行命令 top -h ,就可以查看到消耗资源最高的线程
1 top - 21:56:13 up 157 days, 4:12, 1 user, load average: 3.23, 2.91, 2.77 2 threads: 215 total, 4 running, 211 sleeping, 0 stopped, 0 zombie 3 %cpu(s): 38.7 us, 5.0 sy, 0.0 ni, 52.7 id, 0.0 wa, 0.0 hi, 3.7 si, 0.0 st 4 kib mem : 16267724 total, 248904 free, 2002304 used, 14016516 buff/cache 5 kib swap: 0 total, 0 free, 0 used. 13902576 avail mem 6 7 pid user pr ni virt res shr s %cpu %mem time+ command 8 13951 admin 20 0 8134276 1.190g 22068 s 8.6 7.7 1110:55 java 9 13952 admin 20 0 8134276 1.190g 22068 s 8.6 7.7 1131:28 java 10 13954 admin 20 0 8134276 1.190g 22068 s 8.6 7.7 1104:06 java 11 13957 admin 20 0 8134276 1.190g 22068 s 8.6 7.7 1102:51 java 12 13969 admin 20 0 8134276 1.190g 22068 s 8.6 7.7 1112:43 java 13 13953 admin 20 0 8134276 1.190g 22068 r 8.3 7.7 1107:54 java 14 13955 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1103:20 java 15 13956 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1108:47 java 16 13958 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1048:05 java 17 13959 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1101:41 java 18 13960 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1109:44 java 19 13963 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1111:32 java 20 13964 admin 20 0 8134276 1.190g 22068 r 8.3 7.7 1118:04 java 21 13968 admin 20 0 8134276 1.190g 22068 r 8.3 7.7 1102:15 java 22 13970 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1102:35 java 23 13971 admin 20 0 8134276 1.190g 22068 s 8.3 7.7 1109:01 java 24 13950 admin 20 0 8134276 1.190g 22068 s 8.0 7.7 1077:14 java
3. 查看这个线程所有系统调用
strace -p 13952
上一篇: loggin(日志模块)