java应用内存占用排查(Java application memory usage troubleshooting)

1.查看进程pid

root@crm-prod-68ff6f4b6-79wdq:/# top
top - 15:28:48 up 170 days,  4:38,  0 users,  load average: 1.60, 1.15, 1.60
Tasks:   3 total,   1 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.0 us,  3.4 sy,  0.0 ni, 87.8 id,  0.7 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem : 32110724 total,  2307952 free, 24300924 used,  5501848 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  7535196 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                           
      1 root      20   0 13.109g 7.608g  18584 S   6.4 24.8 104:08.20 java                                                                                              
  78415 root      20   0   20036   3620   2980 S   0.0  0.0   0:00.00 bash                                                                                              
  78431 root      20   0   42792   3480   2996 R   0.0  0.0   0:00.00 top      

2.查看该pid线程

root@crm-prod-68ff6f4b6-79wdq:/# ps p 1 -L -o pcpu,pmem,pid,tid,time,tname,cmd
%CPU %MEM     PID     TID     TIME TTY      CMD
...
0.0 24.8       1      78278   00:00:00 ?     java -Xms4000m -Xmx7300m -javaagent:/data/prometheus/jmx_exporter/jmx_prometheus_javaagent-0.15.0.jar=9000:...

3.查看内存使用的堆栈

使用jstack查看
我们挑选了TID=78278的线程进行分析
在分析前需要将78278这个id转换为16进制,方便查找信息

root@crm-prod-68ff6f4b6-79wdq:/# printf "%x\n" 78278
131c6

16进制为4409
将pid为78278的应用打印到日志中
[root@localhost ~]# jstack -l 78278 > jstack.log

4.查看内存堆栈信息

root@crm-prod-68ff6f4b6-79wdq:/# vin jstack.log 

找到 131c6
————————

1. View process PID

root@crm-prod-68ff6f4b6-79wdq:/# top
top - 15:28:48 up 170 days,  4:38,  0 users,  load average: 1.60, 1.15, 1.60
Tasks:   3 total,   1 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.0 us,  3.4 sy,  0.0 ni, 87.8 id,  0.7 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem : 32110724 total,  2307952 free, 24300924 used,  5501848 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  7535196 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                           
      1 root      20   0 13.109g 7.608g  18584 S   6.4 24.8 104:08.20 java                                                                                              
  78415 root      20   0   20036   3620   2980 S   0.0  0.0   0:00.00 bash                                                                                              
  78431 root      20   0   42792   3480   2996 R   0.0  0.0   0:00.00 top      

2. View the PID thread

root@crm-prod-68ff6f4b6-79wdq:/# ps p 1 -L -o pcpu,pmem,pid,tid,time,tname,cmd
%CPU %MEM     PID     TID     TIME TTY      CMD
...
0.0 24.8       1      78278   00:00:00 ?     java -Xms4000m -Xmx7300m -javaagent:/data/prometheus/jmx_exporter/jmx_prometheus_javaagent-0.15.0.jar=9000:...

3. View the stack of memory usage

使用jstack查看
我们挑选了TID=78278的线程进行分析
在分析前需要将78278这个id转换为16进制,方便查找信息

root@crm-prod-68ff6f4b6-79wdq:/# printf "%x\n" 78278
131c6

16进制为4409
将pid为78278的应用打印到日志中
[root@localhost ~]# jstack -l 78278 > jstack.log

4. View memory stack information

root@crm-prod-68ff6f4b6-79wdq:/# vin jstack.log 

找到 131c6