ps_mem Shows Per-Program Memory Usage On Linux

ps_mem is a tool for checking per-program core memory usage on Linux. It works with both Python 2 and 3, and besides Python it has no dependencies.

Unlike many other tools that report memory usage per process, ps_mem reports the RAM usage of programs. For example it shows how much RAM is used by all Chromium processes combined. The program developer notes that the ps_mem name is used for backwards compatibility, but a more accurate name would be coremem.

The displayed RAM is calculated by adding the sum of private RAM and the sum of shared RAM for a program processes.

Running ps_mem with no arguments shows a list programs and their RAM usage in ascendant order (from the lowest RAM usage to the highest). For each program it shows the private, shared, and total used RAM, as well as the number of processes. Swap information for each program can be shown as well, by using the -S option (sudo ps_mem -S).

This is the ps_mem output (trimmed since it was quite long):

logix@logix-desktop:~$ sudo ps_mem
 Private  +   Shared  =  RAM used Program

192.0 KiB +   6.5 KiB = 198.5 KiB nvidia-persistenced
220.0 KiB +   9.5 KiB = 229.5 KiB vnstatd
232.0 KiB +   7.5 KiB = 239.5 KiB acpid
232.0 KiB +  18.5 KiB = 250.5 KiB atd
236.0 KiB +  41.5 KiB = 277.5 KiB blkmapd
..........................................................
 35.3 MiB +   4.3 MiB =  39.6 MiB goa-daemon
 52.3 MiB +  24.0 MiB =  76.3 MiB Xorg (2)
 81.4 MiB +  13.7 MiB =  95.0 MiB systemd-journald
364.8 MiB +  26.0 MiB = 390.8 MiB gnome-shell (2)
  1.2 GiB + 121.0 MiB =   1.3 GiB firefox-trunk (11)
---------------------------------
                          7.6 GiB
=================================

ps_mem can also show a per-process memory usage instead of showing it on a per-program basis, by using it with the -d option (sudo ps_mem -d).

The tool allows filtering the results by PID. An useful use-case example is shown in the ps_mem README - restricting the ps_mem output to the current user:

sudo ps_mem -p $(pgrep -d, -u $USER)

These are the options supported by ps_mem:

$ ps_mem --help
Usage: ps_mem [OPTION]...
Show program core memory usage

  -h, -help                   Show this help
  -p [,pid2,...pidN]     Only show memory usage PIDs in the specified list
  -s, --split-args            Show and separate by, all command line arguments
  -t, --total                 Show only the total value
  -d, --discriminate-by-pid   Show by process rather than by program
  -S, --swap                  Show swap information
  -w                       Measure and show process memory every N seconds

Install ps_mem


ps_mem is available in the repositories for Fedora, RHEL, CentOS and Arch Linux. You can install it using as follows.

Fedora:

sudo dnf install ps_mem

CentOS / RHEL:

sudo yum install ps_mem

Arch Linux:

sudo pacman -S ps_mem

On other Linux distributions like Debian, Ubuntu, etc., that don't have ps_mem in the repositories, you can download the Python script and install it to /usr/local/bin using:

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

[[Edit]] On newer Ubuntu releases, ps_mem will fail to work because it uses "#!/usr/bin/env python", and not "#!/usr/bin/env python3". Fix this using: 

sudo sed -i 's/env python$/env python3/' /usr/local/bin/ps_mem

You may also install ps_mem using PIP (pip install ps_mem).

Use it by running sudo ps_mem, or type ps_mem --help to see the available options.

via @m_wimpress