Print Friendly, PDF & Email

 
REQUEST

I wish to know how many memory every process uses on a RedHat server.
 

 
SOLUTION

As all we know every process on a server allocates some amount of memory for itself to be able to function correctly. If a process fails to allocate enough memory, then the process cannot be created and the application won’t be able to start.

If there are any issues due to insufficient available free memory on a server you will wish to know which processes have eaten most of the memory of your server. For this purpose there are lots of built in utilities like top, ps, vmstat etc., but most of them does not provide necessary information about the memory used by a process. For instance, why the ps is not an accurate tool to use for this intent.

Most useful utility nowadays is the smem which provide all necessary information about memory usage by a process. This post won’t cover the utility as there are lots of information about it on the internet. The smem is a useful utility for this intent so you’d better use it.

But what if there is no option to install the smem on your server? How to identify memory usage in this case?
So, in this post I want to introduce the script memu.sh which provides similar information to the smem without installing anything on a server.

The memu.sh is the bash script which parses /proc/[pid]/smaps files and returns aggregated data. The script can be executed by any users, but only root can display information for all processes of a server. Non-root users can display details about owned processes only.

Examples


##########################################
## Download the memu.sh script and convert it from Dos to Unix format
##########################################
{
wget http://dbpilot.net/wp-content/uploads/2020/06/memu.v002.zip
unzip memu*.zip
chmod 500 memu.sh
dos2unix memu.sh
}

## Note: It's mandatory to convert the script to  Unix format.
## Otherwise the following error will appear right after the script has been run

[root@word ~]# ./memu.sh
-bash: ./memu.sh: /bin/bash^M: bad interpreter: No such file or directory


##########################################
## Display memory usage of all processes. Sort by PID (default)
##########################################

[root@word ~]# ./memu.sh
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
     Process   Username        Swapped(Mb)     Unique(Mb)     Shared(Mb)        Rss(Mb)        Pss(Mb)      Command
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
           1   root                    0.0            1.2            9.0           10.2            3.1   systemd

...
        2612   root                    0.0            0.6            3.2            3.8            0.8   bash
       16222   root                    0.0            0.2            2.8            3.0            0.4   memu.sh
       19076   root                    0.0            0.3            9.6            9.9            1.7   sshd
       19078   root                    0.0            0.3            5.3            5.6            1.1   sshd
       19079   root                    0.0            0.6            3.1            3.7            0.8   bash
       19100   root                    0.0            0.9            5.5            6.4            1.3   su
       19101   oracle                  0.0            0.6            3.3            3.9            0.8   bash
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
       Total                           0.0          217.3          427.9          645.6          277.3


##########################################
## Display memory usage of all processes. Sort output by the Unique column
## Permitted values : Pid -> pr, Swapped -> sw, Unique -> u, Shared -> s, Rss -> r, Pss -> p
##########################################

[root@word ~]# ./memu.sh sort=u
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
     Process   Username        Swapped(Mb)     Unique(Mb)     Shared(Mb)        Rss(Mb)        Pss(Mb)      Command
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
...
         833   root                    0.0            5.7           11.8           17.6            7.6   NetworkManager
         830   root                    0.0           14.1            9.1           23.2           15.9   polkitd
         847   root                    0.0           19.7           10.6           30.3           21.2   tuned
         888   root                    0.0           25.4           11.1           36.5           27.3   sssd_nss
         936   mysql                   0.0           88.0            6.5           94.6           88.9   mysqld
------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
       Total                           0.0          217.3          428.1          645.8          277.4


##########################################
## Display details about the process with pid 936. 
##########################################

[root@word ~]# ./memu.sh pid=936
--------------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
Date                        Process   Username        Swapped(Mb)     Unique(Mb)     Shared(Mb)        Rss(Mb)        Pss(Mb)      Command
--------------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
28 Jun 2020 01:32:11            936   mysql                   0.0           88.0            6.5           94.6           88.9   mysqld


##########################################
## Display details about the process with pid 936. Print new line every 5 seconds.
##########################################

[root@word ~]# ./memu.sh pid=936 watch=5
--------------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
Date                        Process   Username        Swapped(Mb)     Unique(Mb)     Shared(Mb)        Rss(Mb)        Pss(Mb)      Command
--------------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------   ------------
28 Jun 2020 01:32:31            936   mysql                   0.0           88.0            6.5           94.6           88.9   mysqld
28 Jun 2020 01:32:36            936   mysql                   0.0           88.0            6.5           94.6           88.9   mysqld
28 Jun 2020 01:32:41            936   mysql                   0.0           88.0            6.5           94.6           88.9   mysqld
^C

##########################################
## Display the script's help
##########################################

[root@word ~]# ./memu.sh help

 
 

Version  : 9:54 PM 6/29/2020
Release  : CentOS Linux release 8.1.1911 (Core)