Five ways to query the memory occupied by the process under Linux! Who else wouldn’t?

Time:2021-7-25

Article source: https://mp.weixin.qq.com/s/-kleYkSYFb39WcWR_ QRrvw

Summary of methods for querying the memory occupied by processes under Linux. Suppose there is a “PHP CGI” process with a process ID of “25282”. Now you want to query the memory occupied by the process. There are many tools for viewing under the Linux command line. Now we summarize several common methods.

 

Status through process

[[email protected]_u ~]# cat /proc/25282/status 

Name: php-cgi  

State: S (sleeping)  

Tgid: 25282  

Pid: 25282  

PPid: 27187  

TracerPid: 0  

Uid: 99 99 99 99  

Gid: 99 99 99 99  

Utrace: 0  

FDSize: 256  

Groups: 99  

VmPeak: 496388 kB  

VmSize: 438284 kB  

VmLck: 0 kB  

VmHWM: 125468 kB  

VmRSS: 113612 kB  

VmData: 92588 kB  

VmStk: 100 kB  

VmExe: 6736 kB  

VmLib: 18760 kB  

VmPTE: 528 kB  

VmSwap: 0 kB  

Threads: 1  

SigQ: 0/46155  

SigPnd: 0000000000000000  

ShdPnd: 0000000000000000  

SigBlk: 0000000000000000  

SigIgn: 0000000000001000  

SigCgt: 0000000184000004  

CapInh: 0000000000000000  

CapPrm: 0000000000000000  

CapEff: 0000000000000000  

CapBnd: ffffffffffffffff  

Cpus_allowed: f  

Cpus_allowed_list: 0-3  

Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 

Mems_allowed_list: 0  

voluntary_ctxt_switches: 68245  

nonvoluntary_ctxt_switches: 15751 

Vmrss: 113612 KB indicates the occupied physical memory

Through PMAP

[[email protected]_u ~]# pmap -x 25282 

25282: /usr/local/php/bin/php-cgi –fpm –fpm-config /usr/local/php/etc/php-fpm.conf  

Address Kbytes RSS Dirty Mode Mapping  

0000000000400000 6736 2692 0 r-x– php-cgi  

0000000000c93000 264 196 120 rw— php-cgi 

0000000000cd5000 60 48 48 rw— [ anon ]  

. . .  

00007fd6226bc000 4 4 4 rw— ld-2.12.so  

00007fd6226bd000 4 4 4 rw— [ anon ]  

00007fff84b02000 96 96 96 rw— [ stack ]  

00007fff84bff000 4 4 0 r-x– [ anon ]  

ffffffffff600000 4 0 0 r-x– [ anon ]  

—————- —— —— ——  

total kB 438284 113612 107960 

Key information points

1. Process ID

2. Start the command “/ usr / local / PHP / bin / PHP CGI — FPM — FPM config / usr / local / PHP / etc / PHP FPM. Conf”

3. RSS: physical memory occupied 113612kb

 

Via smaps

[[email protected]_u ~]# cat /proc/25282/smaps | grep ‘^Rss:’ \  

| awk ‘{sum +=$2} END{print sum}’ 

113612 

The actual occupied physical memory is 113612

Via PS command

[[email protected]_u ~]# ps -e -o ‘pid,comm,args,pcpu,rsz,vsz,stime,user,uid’ \  

| awk ‘$1 ~ /25282/’  

25282 php-cgi /usr/local/php/bin/php-cgi 0.0 113612 438284 Oct09 nobody 99 

Awk filters the 25282 process number and obtains that the memory size of column 5 “rsz” is “113612”

Output the physical memory occupied by PHP CGI process and sort it from high to low

[[email protected]_u ~]# ps -e -o ‘pid,comm,args,pcpu,rsz,vsz,stime,user,uid’ \  

| grep php-cgi | sort -k5nr 

Output results

23946 php-cgi /usr/local/php/bin/php-cgi 0.0 129540 440000 Oct06 nobody 99  

24418 php-cgi /usr/local/php/bin/php-cgi 0.0 129336 437684 Oct06 nobody 99  

18973 php-cgi /usr/local/php/bin/php-cgi 0.0 129268 440176 Oct06 nobody 99  

17219 php-cgi /usr/local/php/bin/php-cgi 0.0 126588 439840 Oct06 nobody 99  

6996 php-cgi /usr/local/php/bin/php-cgi 0.0 124876 438104 Oct09 nobody 99  

23850 php-cgi /usr/local/php/bin/php-cgi 0.0 122984 440036 Oct09 nobody 99  

28310 php-cgi /usr/local/php/bin/php-cgi 0.0 122920 436456 Oct09 nobody 99 

Where rsz is the actual memory. The above example implements sorting by memory, from large to small.

 

Columns output by the top command

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  

25282 nobody 20 0 428m 110m 93m S 0.0 1.9 0:34.42 php-cgi 

Output column information

 PID 25282

User nobody

Virtual memory 428m

Physical memory 110m 110 * 1024 = 112640 “basically consistent with the previously calculated value”

Shared memory 93m

Percentage of physical memory and total memory used by the process 1.9%

PID: ID of the process

User: process owner

PR: the priority of the process. The smaller the priority, the more priority to be executed

Ninice: value

Virt: virtual memory occupied by the process

Res: physical memory occupied by the process

SHR: shared memory used by the process

S: Status of the process. S indicates sleep, R indicates running, Z indicates dead state, and N indicates that the priority value of the process is negative

%CPU: the utilization rate of the CPU occupied by the process

%MEM: percentage of physical memory and total memory used by the process

Time +: the total CPU time occupied after the process is started, that is, the cumulative value of the CPU usage time occupied.

Command: process startup command name

Press P

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  

16036 root 20 0 8902m 8.6g 480 R 100.0 36.6 0:33.15 redis-server  

12934 root 20 0 8902m 8.6g 1072 S 5.5 36.6 285:37.81 redis-server  

969 root 20 0 0 0 0 D 4.2 0.0 277:14.85 flush-252:16  

1304 root 23 3 1689m 50m 3264 S 4.2 0.2 1445:03 xs-searchd  

1294 root 20 0 14928 928 584 S 3.5 0.0 635:05.31 xs-indexd  

1287 nobody 20 0 12884 772 576 S 2.8 0.0 833:11.42 dnsmasq  

1302 root 23 3 1113m 39m 3244 S 0.7 0.2 1437:57 xs-searchd 

4444 www 20 0 280m 43m 884 S 0.7 0.2 27:43.92 nginx  

1 root 20 0 19232 1160 868 S 0.0 0.0 0:06.75 init 

Press P. to sort by CPU. By default, it is also sorted by CPU

Press M

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  

12934 root 20 0 8902m 8.6g 1072 S 6.0 36.6 285:39.77 redis-server  

16036 root 20 0 8902m 8.6g 480 R 100.0 36.6 1:11.42 redis-server  

1236 www 20 0 1053m 209m 6556 S 0.0 0.9 4:40.70 php-cgi  

1231 www 20 0 1034m 146m 6536 S 0.0 0.6 4:20.82 php-cgi  

1184 www 20 0 1043m 119m 6584 S 0.0 0.5 4:21.85 php-cgi 

Press M. Indicates sorting by occupied memory. The first column of redis servers occupies 8.6g of memory. This memory and redis info

[[email protected]_u ~]# redis-cli info memory  

Memory  

used_memory_human:8.32G 

Basically the same.

[[email protected]_u ~]# top -u www  

top – 22:09:01 up 67 days, 14:16, 1 user, load average: 0.61, 0.90, 0.98  

Tasks: 283 total, 2 running, 281 sleeping, 0 stopped, 0 zombie  

Cpu(s): 3.9%us, 1.0%sy, 0.5%ni, 89.7%id, 4.6%wa, 0.0%hi, 0.3%si, 0.0%st  

Mem: 24542176k total, 21130060k used, 3412116k free, 1750652k buffers  

Swap: 524280k total, 0k used, 524280k free, 4039732k cached  

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  

681 www 20 0 855m 25m 5796 S 0.0 0.1 0:47.00 php-cgi  

1181 www 20 0 887m 57m 6484 S 0.0 0.2 4:41.66 php-cgi  

1183 www 20 0 864m 34m 6320 S 0.0 0.1 3:52.39 php-cgi  

1184 www 20 0 1043m 119m 6584 S 0.0 0.5 4:21.85 php-cgi  

1185 www 20 0 869m 39m 6376 S 0.0 0.2 3:57.84 php-cgi  

1186 www 20 0 886m 56m 6244 S 0.0 0.2 3:44.75 php-cgi  

1187 www 20 0 926m 66m 6480 S 0.0 0.3 4:16.12 php-cgi  

1188 www 20 0 890m 60m 6288 S 0.0 0.3 4:13.35 php-cgi  

1189 www 20 0 892m 62m 6408 S 0.0 0.3 4:06.60 php-cgi 

-U specifies the user. PHP CGI occupies about 60m of memory

 

 

A method of sorting by how much memory the process consumes

Via PS command

The first method

ps -e -o ‘pid,comm,args,pcpu,rsz,vsz,stime,user,uid’ | sort -k5nr 

The second method

ps -e -o ‘pid,comm,args,pcpu,rsz,vsz,stime,user,uid’ –sort -rsz 

Output results

[[email protected]_u ~]# ps -e -o ‘pid,comm,args,pcpu,rsz,vsz,stime,user’ | sort -k5nr  

23946 php-cgi /usr/local/php/bin/php-cgi 0.0 129540 440000 Oct06 nobody  

24418 php-cgi /usr/local/php/bin/php-cgi 0.0 129336 437684 Oct06 nobody  

18973 php-cgi /usr/local/php/bin/php-cgi 0.0 129268 440176 Oct06 nobody  

17219 php-cgi /usr/local/php/bin/php-cgi 0.0 126588 439840 Oct06 nobody  

6996 php-cgi /usr/local/php/bin/php-cgi 0.0 125056 438104 Oct09 nobody  

23850 php-cgi /usr/local/php/bin/php-cgi 0.0 122984 440036 Oct09 nobody 

Parameter resolution:

-E display all processes

-O customized display information

PID process ID

Comm process name

Args start command

Pcpu CPU percentage

Rsz occupied physical memory size

Size of virtual memory occupied by vsz

Stime process start time

User start user

Take the first behavior as an example

Process ID   twenty-three thousand nine hundred and forty-six

Process name   php-cgi

Start command  / usr/local/php/bin/php-cgi

CPU occupied   0

Occupy physical memory   one hundred and twenty-nine thousand five hundred and forty

Occupy virtual memory   four hundred and forty thousand

Start time   Oct06

Start user   nobody

Through the top command

The top command is output by CPU by default. It can be sorted and displayed according to the memory occupation by the letter “m”

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  

23946 nobody 20 0 429m 126m 107m S 0.0 2.2 1:15.01 php-cgi  

24418 nobody 20 0 427m 126m 109m S 0.0 2.2 1:19.56 php-cgi  

18973 nobody 20 0 429m 126m 107m S 0.0 2.2 1:20.18 php-cgi  

17219 nobody 20 0 429m 123m 104m S 0.0 2.1 1:23.60 php-cgi  

6996 nobody 20 0 427m 122m 105m S 0.0 2.1 1:05.27 php-cgi  

23850 nobody 20 0 429m 120m 101m S 0.0 2.1 1:02.43 php-cgi 

Introduction to output parameters

PID: ID of the process

User: process owner

Virt: virtual memory occupied by the process

Res: physical memory occupied by the process

SHR: shared memory used by the process

S: Status of the process. S indicates sleep, R indicates running, Z indicates dead state, and N indicates that the priority value of the process is negative

%CPU: the utilization rate of the CPU occupied by the process

%MEM: percentage of physical memory and total memory used by the process

Time +: the total CPU time occupied after the process is started, that is, the cumulative value of the CPU usage time occupied.

By comparing the process ID “23946”, the output results of the top command and PS command are basically consistent.

 

last

It is especially recommended to share the high-quality content of C / C + + and algorithms, learning and communication, technical discussion, interview guidance, resume modification… As well as learning materials such as super many source materials and zero basic videos waiting for you!

If you haven’t paid attention, you can long Click to pay attention: