graph process memory

graph a Linux process's memory usage

Sometimes you want to run a Linux program, keep it in the foreground, and still watch how much memory it is using. The little memgraph script in this toolbox takes a process id, logs the process's RSS memory once per second, and opens a live-refreshing gnuplot graph.

install the dependencies

The script checks for its own dependencies before it starts. It needs ps, date, awk, and gnuplot. On most Linux systems only gnuplot is likely to be missing.

sudo apt install gnuplot procps gawk coreutils

On Fedora:

sudo dnf install gnuplot procps-ng gawk coreutils

On Arch:

sudo pacman -S gnuplot procps-ng gawk coreutils

use the tool

./content/tools/linux/memgraph 817723

By default it writes a log named after the process id, like mem-817723.log. You can choose the log path too:

./content/tools/linux/memgraph 817723 ./mem.log

The log format is two columns:

unix_timestamp rss_mb

start a process and print its pid

If you do not mind putting the program in the background, the shell gives you the pid with $!:

./your_program &
pid=$!
echo "PID: $pid"

If you want the program to stay in the foreground, run it through a tiny wrapper:

sh -c 'echo "PID: $$"; exec ./your_program'

The exec part replaces the wrapper shell with your program, so the printed pid becomes your program's pid.

With arguments:

sh -c 'echo "PID: $$"; exec "$@"' sh ./your_program arg1 arg2

Then, from a second terminal:

./content/tools/linux/memgraph PID_FROM_THE_FIRST_TERMINAL

script


edit this page