How to Check Memory Usage per Process on Linux

Introduction

Before we begin talking about how to check memory usage per process on Linux, let's briefly understand – What is ps command?

The ps command on Linux is used to view and analyze running processes on a system. It stands for "Process Status" and provides detailed information about processes such as their IDs, memory usage, CPU usage, and more.

By using the ps command, users can monitor and manage processes effectively, ensuring optimal system performance. Whether troubleshooting or tracking resource usage, understanding the ps command is essential for Linux sysadmins and power users.

In this tutorial, you will check memory usage per process on Linux. We will also address a few FAQs on how to check memory usage per process on Linux.

Advantages of ps command

  1. Process Monitoring: The ps command enables users to monitor and track running processes on Linux systems, allowing for efficient resource management.
  2. Resource Usage: With ps, users can view CPU and memory usage of processes, helping to identify resource-intensive tasks and optimize performance.
  3. Process Identification: By displaying process IDs, ps helps users uniquely identify processes for further analysis or termination if necessary.
  4. Filtering and Sorting: ps supports various options for filtering and sorting processes based on different criteria, making it easier to locate specific processes.
  5. Customization: The ps command offers flexibility, allowing users to tailor the output according to their needs, displaying relevant information about processes.

1) Find out the top memory-consuming process on Linux using the ps command

To report a snapshot of running processes, you may use the ps command. ps is shorthand for "process status" in command prompts.

This is a common Linux program that queries the system for information about currently active processes.

You may use it to see which commands are now being executed together with their PIDs, owner names, priorities, and full paths.

The following ps command will examine Linux memory utilization for each running process:

# ps aux --sort -rss | head

USER       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 -s malloc,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 process

To add precise information on such processes in the output, use the ps command format shown below:

# ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head

  PID  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.pid -f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -s malloc,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

Use the ps command format shown below if you simply want to view the command name rather than the command's absolute path:

# ps -eo pid,ppid,%mem,%cpu,comm --sort=-%mem | head

  PID  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 cwpsrv

2) How to check memory usage per process on Linux using the ‘top’ command

The most effective and often used command for tracking Linux system performance is top.

The user interface shows the processes currently being processed by the system in real-time.

To see which Linux processes are using the most memory, you may execute the top command in batch mode.

Fixing the system's performance problem also requires proper interpretation of the top command's output.

# top -c -b -o +%MEM | head -n 20 | tail -15

  PID 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.pid -f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -s malloc,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 cwpsrv: worker process
 3034 root      20   0   86704  36740   1452 S   0.0  0.9   0:00.09 cwpsrv: master process /usr/local/cwpsrv/bin/cwpsrv
 1067 nobody    20   0 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

Use the top command format below if you simply want to view the command name rather than the command's absolute path:

# top -b -o +%MEM | head -n 20 | tail -15

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1064 mysql     20   0  886076 210340   8388 S   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      20   0   86704  36740   1452 S   0.0  0.9   0:00.09 cwpsrv
 1067 nobody    20   0 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

3) Bonus Tips: Checking memory usage on Linux by process using the ps_mem command

A program's core memory use may be seen using the ps_mem utility (not per process).

Using this utility, you can see which programs are using the most memory.

It determines how much shared and private memory is being utilized by a program, and then returns the total amount of memory being used in the best possible manner.

Install it using these commands:

sudo wget -qO /usr/local/bin/ps_mem https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py

sudo chmod a+x /usr/local/bin/ps_mem

sudo ln -s /usr/bin/python3 /usr/bin/python

ps_mem --version

The logic it uses to determine RAM utilization is as follows. Total RAM = sum (private RAM for program processes) + sum (shared RAM for program processes).

The ps_mem command below displays how much memory each process on Linux is using.

# ps_mem


 Private  +   Shared  =  RAM used    Program 
128.0 KiB +  27.5 KiB = 155.5 KiB    agetty
228.0 KiB +  47.0 KiB = 275.0 KiB    atd
284.0 KiB +  53.0 KiB = 337.0 KiB    irqbalance
380.0 KiB +  81.5 KiB = 461.5 KiB    dovecot
364.0 KiB + 121.5 KiB = 485.5 KiB    log
520.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.0 KiB + 524.0 KiB = 820.0 KiB    avahi-daemon (2)
772.0 KiB +  51.5 KiB = 823.5 KiB    systemd-logind
940.0 KiB + 162.5 KiB =   1.1 MiB    dbus-daemon
  1.1 MiB +  99.0 KiB =   1.2 MiB    pure-ftpd
  1.2 MiB + 100.5 KiB =   1.3 MiB    master
  1.3 MiB + 198.5 KiB =   1.5 MiB    pickup
  1.3 MiB + 198.5 KiB =   1.5 MiB    bounce
  1.3 MiB + 198.5 KiB =   1.5 MiB    pipe
  1.3 MiB + 207.5 KiB =   1.5 MiB    qmgr
  1.4 MiB + 198.5 KiB =   1.6 MiB    cleanup
  1.3 MiB + 299.5 KiB =   1.6 MiB    trivial-rewrite
  1.5 MiB + 145.0 KiB =   1.6 MiB    config
  1.4 MiB + 291.5 KiB =   1.6 MiB    tlsmgr
  1.4 MiB + 308.5 KiB =   1.7 MiB    local
  1.4 MiB + 323.0 KiB =   1.8 MiB    anvil (2)
  1.3 MiB + 559.0 KiB =   1.9 MiB    systemd-journald
  1.8 MiB + 240.5 KiB =   2.1 MiB    proxymap
  1.9 MiB + 322.5 KiB =   2.2 MiB    auth
  2.4 MiB +  88.5 KiB =   2.5 MiB    systemd
  2.8 MiB + 458.5 KiB =   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.0 KiB = 104.9 MiB    named
112.2 MiB + 479.5 KiB = 112.7 MiB    cache-main
 69.4 MiB +  58.6 MiB = 128.0 MiB    nginx (4)
203.4 MiB + 309.5 KiB = 203.7 MiB    mysqld
---------------------------------
                        775.8 MiB
=================================

FAQs to Check Memory Usage per Process on Linux

What is the command to check the memory usage of a specific process? 

To check the memory usage of a particular process, you can use the pmap command followed by the process ID. This command provides detailed information about the memory usage of the specified process.

Can top command be used to check memory usage per process? 

Yes, the top command can be used to check memory usage per process. While top displays the overall system information, it also provides a detailed view of memory usage for each process, including memory consumption and percentages.

Can I check memory usage per process remotely on a Linux server? 

Yes, you can use tools like ssh to access a remote Linux server and then execute the desired commands, such as ps or top, to check memory usage per process on that server.

How can I monitor changes in memory usage over time? 

Tools like sar (System Activity Reporter) or vmstat can be used to monitor changes in memory usage over time. These utilities collect and report system statistics, including memory usage, at specified intervals.

What should I do if I find a process consuming excessive memory? 

If you discover a process consuming excessive memory, you can investigate by using tools like strace or gdb to analyze its behavior. Killing or restarting the process might be necessary to reclaim memory.

Are there graphical tools available to check memory usage per process? 

Yes, there are graphical tools like gnome-system-monitor, KSysGuard, and htop that provide a visual representation of memory usage per process. These tools offer a more user-friendly way to monitor memory consumption on Linux systems.

How can I check the memory usage per process on Linux? 

You can use the ps command with the --sort option to display the memory usage of processes. By sorting processes based on memory usage, you can easily identify the memory-hungry ones.

Conclusion

In this tutorial, you learned to utilize the 'top' and 'ps' utilities to find the Linux processes using the most RAM.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.