Many times, you may come across a system that consumes too much memory. If this is the case, your best bet is to identify the processes on your Linux machine that are consuming too much memory. I’m sure you’ve already run the command below to check. If not, what other commands have you tried? I hope you can update this article in the comments and it may help other users.

Using the top and ps commands, you can easily identify this situation. I used to use both of these commands together, and they gave me the same result. So I suggest you just pick one of them and use it.

1) How to use the ps command to find the most memory consuming process in Linux

The ps command is used to report a snapshot of the current process. The ps command stands for “process state.” This is a standard Linux application for finding information about processes running on a Linux system.

It is used to list the currently running processes and their process IDS (PID), process owner name, process priority (PR), and the absolute path of running commands.

The ps command format below gives you more information about the process that consumes the most memory.

# ps aux --sort -rss | headUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 1064 3.2 5.4 886076 209988? Ssl Oct25 62:40 /usr/sbin/mysqld Varnish 23396 0.0 2.9 286492 115616? SLl Oct25 0:42 /usr/sbin/varnishd -P /var/run/varnish.pid-f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -sMalloc,256M named 1105 0.0 2.7 311712 108204? Ssl Oct25 0:16 /usr/sbin/named -u named -c /etc/named.conf nobody 23377 0.2 2.3 153096 89432? S Oct25 4:35 nginx: worker process nobody 23376 0.1 2.1 147096 83316? S Oct25 2:18 nginx: worker process root 23375 0.0 1.7 131028 66764? Ss Oct25 0:01 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nobody 23378 0.0 1.6 130988 64592? S Oct25 0:00 nginx: Cache Manager process root 1135 0.0 0.9 86708 37572? S 05:37 0:20 CWPSRV: worker process root 1133 0.0 0.9 86708 37544? S 05:37 0:05 cwpsrv: worker processCopy the code

Use the following ps command format to show only specific information about the memory consumption process in the output.

# ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | headPID PPID %MEM %CPU CMD 1064 1 5.4 3.2 /usr/sbin/mysqld 23396 23386 2.9 0.0 /usr/sbin/varnishd -p /var/run/varnish-f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -sMalloc,256M 1105 1 2.7 0.0 /usr/sbin/named -u named -c /etc/named.conf 23377 23375 2.3 0.2 nginx: Worker process 23376 23375 2.1 0.1 nginx: worker process 3625 977 1.9 0.0 /usr/local/bin/php-cgi /home/daygeekc/public_html/index.php
23375     1  1.7  0.0 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
23378 23375  1.6  0.0 nginx: cache manager process
 1135  3034  0.9  0.0 cwpsrv: worker process
Copy the code

If you only want to see the command name instead of the absolute path to the command, use the ps command format below.

# ps -eo pid,ppid,%mem,%cpu,comm --sort=-%mem | headPID PPID %MEM %CPU COMMAND 1064 1 5.4 3.2 mysqld 23396 23386 2.9 0.0 cache-main 1105 1 2.7 0.0 named 23377 23375 2.3 0.2 Nginx 23376 23375 2.1 0.1 nginx 23375 1 1.7 0.0 nginx 23378 23375 1.6 0.0 nginx 1135 3034 0.9 0.0 CWPSRV 1133 3034 0.9 0.0 CWPSRVCopy the code

2) How to use the top command to find the most memory consuming process in Linux

The Linux top command is the best and best-known command for monitoring Linux system performance. It displays a real-time view of running system processes on an interactive interface. However, if you want to find the process that consumes the most memory, use the top command in batch mode.

You should properly understand the top command output to solve performance problems in your system.

# top -c -b -o +%MEM | head -n 20 | tail -15PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1064 mysql 20 0 886076 209740 8388 S 0.0 5.4 62:41.20 /usr/sbin/mysqld 23396 Varnish 20 0 286492 115616 83572 S 0.0 3.0 0:42.24 /usr/sbin/varnishd -p /var/run/varnish-f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -sMalloc,256M 1105 named 20 0 311712 108204 2424 S 0.0 2.8 0:16.41 /usr/sbin/named -u named -c /etc/named.conf 23377 Nobody 20 0 153240 89432 2432 S 0.0 2.3 4:35.74 nginx: Worker process 23376 nobody 20 0 147096 83316 2416 S 0.0 2.1 2:18.09 nginx: Worker process 23375 root 20 0 131028 66764 1616 S 0.0 1.7 0:01.07 nginx: Master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 23378 nobody 20 0 130988 64592 592 S 0.0 1.7 0:00.51 nginx: Cache Manager process 1135 root 20 0 86708 37572 2252 S 0.0 1.0 0:20.18 CWPSRV: Worker process 1133 root 20 0 86708 37544 2212 S 0.0 1.0 0:05.94 Worker process root 20 0 86704 36740 1452 S 0.0 0.9 0:00.09 CWPSRV: master process /usr/local/ CWPSRV /bin/cwpsrv 1067 nobody 200 1356200 31588 2352 S 0.0 0.8 0:56.06 /usr/local/apache/bin/httpd -k start
  977 nobody    20   0 1356088  31268   2372 S   0.0  0.8   0:30.44 /usr/local/apache/bin/httpd -k start
  968 nobody    20   0 1356216  30544   2348 S   0.0  0.8   0:19.95 /usr/local/apache/bin/httpd -k start
Copy the code

If you only want to see the command name instead of the absolute path to the command, use the top command format below.

# top -b -o +%MEM | head -n 20 | tail -15PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1064 mysql 20 0 886076 210340 8388s 6.7 5.4 62:40.93 mysqld 23396 Varnish 20 0 286492 115616 83572 S 0.0 3.0 0:42.24 cache-main 1105 Named 20 0 311712 108204 2424 S 0.0 2.8 0:16.41 Named 23377 nobody 20 0 153240 89432 2432 S 13.3 2.3 4:35.74 nginx 23376 nobody 20 0 147096 83316 2416 S 0.0 2.1 2:18.09 Nginx 23375 root 20 0 131028 66764 1616 S 0.0 1.7 0:01.07 nginx 23378 nobody 20 0 130988 64592 592 S 0.0 1.7 0:00.51 Nginx 1135 root 20 0 86708 37572 2252 S 0.0 1.0 0:20.18 CWPSRV 1133 root 20 0 86708 37544 2212 S 0.0 1.0 0:05.94 CWPSRV 3034 root 200 86704 36740 1452 S 0.0 0.9 0:00.09 CWPSRV 1067 nobody 200 1356200 31588 2352 S 0.0 0.8 0:56.04 HTTPD 977 Nobody 20 0 1356088 31268 2372 S 0.0 0.8 0:30.44 HTTPD 968 nobody 20 0 1356216 30544 2348 S 0.0 0.8 0:19.95 HTTPD 968 nobody 20 0 1356216 30544 2348 S 0.0 0.8 0:19.95 HTTPDCopy the code

3) Bonus tip: How do I use ps_mem to find the most memory consuming process in Linux

The PS_MEm program is used to display the core memory used by each program, not each process. This program allows you to check how much memory each program is using. It calculates the amount of private and shared memory according to the program and returns the total memory used in the most appropriate way.

It uses the following logic to calculate memory usage. Total memory usage = sum(dedicated memory usage for application processes) + sum(shared memory usage for application processes).

# ps_memPrivate + Shared = RAM Used Program 128.0kib + 27.5kib = 155.5kib AGetty 228.0kib + 47.0kib = 275.0kib ATD 284.0 Dovecot 364.0kib + 121.5kib = 485.5kiblog520.0 KiB + 65.5 KiB = 585.5 KiB AUDITd 556.0 KiB + 60.5 KiB = 616.5 KiB Systemd-Udevd 732.0 KiB + 48.0 KiB = 780.0 KiB Crond 296.0kib + 524.0kib = 820.0kib Avahi - Daemon (2) 772.0kib + 51.5kib = 823.5kib Systemd - Logind 940.0kib + 162.5KIb = 1.1 MiB Dbus-Daemon 1.1 MiB + 99.0KIb = 1.2 MiB Pure-FTpd 1.2 MiB + 100.5KIb = 1.3 MiB Master 1.3 MiB + 198.5KIb = 1.5 MiB Pickup 1.3 MiB + 198.5KIb = 1.5 MiB bounce 1.3 MiB + 198.5KIb = 1.5 MiB PIPE 1.3 MiB + 207.5KIb = 1.5 MiB QMGR 1.4 MiB + 198.5KIb = 1.6 MiB Cleanup 1.3 MiB + 299.5KIb = 1.6 MiB trivial-rewrite 1.5 MiB + 145.0KIb = 1.6 MiB Config 1.4 MiB + 291.5KIb = 1.6 MiB TLSMGR 1.4 MiB + 308.5KIb = 1.7 MiBlocal1.4 MiB + 323.0 KiB = 1.8 MiB Anvil (2) 1.3 MiB + 559.0KIb = 1.9 MiB SystemD-JournALD 1.8 MiB + 240.5KIb = 2.1 MiB Proxymap 1.9 MiB + 322.5KIb = 2.2 MiB AuTH 2.4 MiB + 88.5KIb = 2.5 MiB Systemd 2.8 MiB + 458.5KIb = 3.2 MiB SMTPD 2.9 MiB + 892.0 KiB = 3.8 MiB Bash (2) 3.3 MiB + 555.5 KiB = 3.8 MiB NetworkManager 4.1 MiB + 233.5 KiB = 4.3 MiB Varnishd 4.0 MiB + 662.0 KiB = 4.7 MiB DHClient (2) 4.3 MiB + 623.5 KiB = 4.9 MiB RSYSLOGD 3.6 MiB + 1.8 MiB = 5.5 MiB SSHD (3) 5.6 MiB + 431.0 KiB = 6.0 MiB Polkitd 13.0 MiB + 546.5 KiB = 13.6 MiB Tuned 22.5 MiB + 76.0 KiB = 22.6 MiB LFD - Sleeping 30.0 MiB + 6.2 MiB = 36.2 MiB PHP-FPM (6) 5.7 MiB + 33.5 MiB = 39.2 MiB CWPSRV (3) 20.1 MiB + 25.3 MiB = 45.4 MiB HTTPD (5) 104.7 MiB + 156.0KIb = 104.9 MiB Named 112.2 MiB + 479.5KIb = 112.7 MiB cache-main 69.4 MiB + 58.6 MiB = 128.0 the MiB nginx (4) 203.4 MiB KiB + 309.5 = 203.7 MiB mysqld -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 775.8 MiB = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

Via: www.2daygeek.com/linux-find-…

Magesh Maruthamuthu (lujun9972

This article is originally compiled by LCTT and released in Linux China