tomcat内存使用情况
预发布阿里云服务器的容器 tomcat会自己无缘无故重启,故引出一些查看tomcat内存使用情况观察的细枝末节:
1️⃣当前端口号进程信息和GC使用情况
(1)显示端口的PID:lsof -i:端口
示例:lsof -i:7074
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 3195 ligang 34u IPv4 37416693 0t0 TCP *:7074 (LISTEN)
(2)gc信息统计:jstat -gcutil PID
示例:jstat -gcutil 3195
S0 S1 E O P YGC YGCT FGC FGCT GCT
12.63 0.00 52.03 78.63 99.13 4148 24.274 200 40.246 64.520
(3)输出线程数:ps -mp PID -o THREAD,tid,time | wc -l
示例:ps -mp 3195 -o THREAD,tid,time | wc -l
2️⃣查看进程内存使用情况及定位到对应程序
(1)内存使用情况:top -p PID
示例:top 3195
top - 15:29:27 up 25 days, 20:05, 2 users, load average: 0.01, 0.05, 0.01
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.1%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8058868k total, 6821684k used, 1237184k free, 181936k buffers
Swap: 2097144k total, 492300k used, 1604844k free, 1897320k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3195 ligang 20 0 4862m 196m 10m S 0.0 2.5 7:57.48 java
(2)找到该进程后,如何定位具体线程或代码呢,首先显示线程列表,并按照CPU占用高的线程排序:
ps -mp PID -o THREAD,tid,time | sort -rn | head -10
示例:ps -mp PID -o THREAD,tid,time | sort -rn | head -10
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
ligang 0.6 - - - - - - 00:07:58
ligang 0.2 19 - futex_ - - 3270 00:02:49
ligang 0.0 19 - inet_c - - 3277 00:00:00
ligang 0.0 19 - inet_c - - 3273 00:00:00
ligang 0.0 19 - inet_c - - 3271 00:00:00
ligang 0.0 19 - inet_c - - 3203 00:00:05
ligang 0.0 19 - futex_ - - 7644 00:00:00
ligang 0.0 19 - futex_ - - 3420 00:00:00
ligang 0.0 19 - futex_ - - 3288 00:00:06
(3)将需要的线程ID转换为16进制格式:printf "%x\n" TID
示例:printf "%x\n" 3270
cc6
(4)最后打印线程的堆栈信息:jstack PID |grep cc6 -A 30
示例:
jstack 2633 |grep e18 -A 30