Create a gist now

Instantly share code, notes, and snippets.

@netj /memusg
Last active Feb 21, 2018

What would you like to do?
memusg -- Measure memory usage of processes
#!/usr/bin/env bash
# memusg -- Measure memory usage of processes
# Usage: memusg COMMAND [ARGS]...
# Author: Jaeho Shin <>
# Created: 2010-08-16
set -um
# check input
[ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }
# TODO support more options: peak, footprint, sampling rate, etc.
# detect operating system and prepare measurement
case `uname` in
Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;;
Linux) sizes() { /bin/ps -o rss= -$1; } ;;
*) echo "`uname`: unsupported operating system" >&2; exit 2 ;;
# monitor the memory usage in the background.
pgid=`ps -o pgid= $$`
while sizes=`sizes $pgid`
set -- $sizes
let peak="sample > peak ? sample : peak"
sleep 0.1
echo "memusg: peak=$peak" >&2
) &
# run the given command
exec "$@"

brassel commented Oct 15, 2010

Hi Jaeho Shin,

Thanks for your easy to use tool!
I would like to use your script for a series of benchmarks and I would like to get the output for each benchmark separately. But somehow all peaks get printed after the whole suite is finished. Can I do something about this?



netj commented Oct 15, 2010

Hi brassel,
I'm so glad to meet a fan of my tool :)

I think you just pointed out a caveat of my script.
This script monitors the rss peak of all processes in the same process group which shares a PGID.
It seems you're running all the benchmarks with this tool within a single shell script, right?
Since processes launched from a non-interactive shell usually uses the same PGID, this tool won't work correctly.
I only considered using this from an interactive shell, but I should fix this limitation as I'll encounter a similar case soon.

For the moment, you can launch each benchmark prefixing it with setsid:
setsid memusg ./benchmark-XYZ ...
Or, if setsid isn't available on your system, then using bash -i (makes a little clutter):
bash -i -c 'memusg ./benchmark-XYZ ...'

Thanks for your feedback!

brassel commented Oct 15, 2010

Thank you very much for the fast answer!
Using "setsid" worked like a charm.

Thankful Greetings,


netj commented Oct 15, 2010

Bernd, I actually updated the script an hour ago so we can now use it without the workaround. :)

brassel commented Oct 15, 2010

As I have read your comments in the script

# TODO support more options: peak, footprint, sampling rate, etc.

I thought you might be interested that for my purposes I have changed by hand

  • the sampling rate
  • the redirection of the resulting output to &2

Everything else was just as needed.

brassel commented Oct 15, 2010

One thing I notice is that I do not seem to be able to stop processes with ^C while using memusg.
What might be the reason for this?

XICO2KX commented Mar 13, 2011

I have a problem using your tool with a multi-threaded program...

This last program when started launches itself a couple of times.
(Different processes with different PIDs but with the same name appear on "top".)

I am using a shell script to launch "memusg". Something like:
[] command

And I run it through "nohup":
nohup &

But by watching "top", the command itself isn't executed!
(Or it shows up and quickly disappears constantly!)

And the output file shows multiple times:
[nohup.out] bash: no job control in this shell

The only processes that I can see running are:
bash command

But if I kill the process (the one initiated by "nohup"):
the command gets executed!

Do you know what is the problem and how can I fix it?
Thank you very much!

dbolser commented Oct 28, 2011

Sorry for being dumb... what is the output? kb? Cheers,


netj commented Oct 31, 2011

@brassel and @XICO2KX I know it's very late, but I'll look into this problem. It seems there're quite a few folks who want to use this in various ways.

gbluma commented Feb 28, 2013

+1 Just used this for some quick profiling. Thanks.

nderevj commented Mar 21, 2013

Nice work! I used this script to do some quick profiling of some PHP scripts.

XICO2KX commented Jul 12, 2013

Just a heads up...
User Jonathan Clark has a reviewed and improved version of this memusg script here:
It seems to have fixed all the problems with child processes, threads and nohup.
The only disadvantage is that it measures the Virtual Size instead.
But you only need to change the expression 'vsize=' to 'rssize=' to get the Peak (High Water Mark) RSS Memory Usage.

kilburn commented Jan 16, 2014

Just FYI, the version linked by @XICO2KX does not work on OSX/BSD (due to differences in the ps binary).

has anyone an idea, what I can do if I have to log a software that only works like this:

executable parameter parameter < input

I could adapt the script, so it only works with that tool and pass the input as a normal parameter, and instead of "just" calling $@ split the arguments and construct the correct call. But I like these kind of monitoring tools to work more generic.

for example if I type

/usr/bin/time -v sort < unsorted_file

I get the sorted output as expected. If I do the same with your script, it does not work

I found the answer:

exec "$@" <&0

should pass the stdin to the command

dbohdan commented Feb 13, 2015

Hi, @netj! I'm a fan of your script and have been using it for a long time. I want to include it in my repository,, to be run from a benchmarking script. Could you state what license it is distributed under?

I second the question of @dbohdan. @netj, what is the license here? MIT? Public domain?

jvollme commented Aug 6, 2015

Hi, i'm trying to use this script to monitor peak RAM usage of scripts I'm running withing an SGE environment on a shared server (using qsub). I've found your script useful and easy to use , so i decided to use it here.
However, the SGE-jobs always fail when I try to run "memusg" with the following error message:
bash: no job control in this shell '': unknown terminal type.
memusg only works if i run it in a local shell (NOT submiting it via qsub) which sadly is not an option for the jobs i want to run.
Is there a way to fix this?


netj commented Nov 10, 2015

@dbohdan @holtgrewe: Thanks for asking! Didn't know this was being used by so many people. I just added the license (Apache 2.0) to the script. You are free to use/include/modify it. Looks like I should try to iron out the issues raised here and there.

tantrev commented Mar 29, 2016

Fantastic tool! Use it quite frequently to profile programs for parallelization. I noticed that sending it to background with a terminating "&" seems to cause problems - in particular, it always closes my shell session. Would it be too difficult to make memusg compatible with background execution? :P

EmilStenstrom commented May 15, 2016

Can I suggest this is moved to an actual repository instead of just a gist? Would make it possible to send pull requests and create issues properly.

mbland commented Aug 20, 2016

FWIW, though it's nearly six years since @brassel asked his first question, just yesterday I wrestled with an issue whereby memusg was hanging in a driver script. I have an extensive comment in my script explaining the issue with bash -i -c "$cmd" causing my script to hang, and how job control (set -m) solves the problem. An answer to Why can't I use job control in a bash script? helped me zero in on the set -m solution.

A reproduction, per my comment:

First create the following script; I'll call it

memusg ls >"foo0.out" 2>&1
memusg ls >"foo1.out" 2>&1


  • Run it as bash and it will hang; run fg to continue.
  • Run it as bash -m and it will complete.

Also try editing the output redirections per the following and running it with bash

  • Remove the first 2>&1 redirection and it will hang.
  • Remove the second 2>&1 redirection and it will complete.
  • Remove both 2>&1 redirection and it will complete.
  • Remove the first >"foo0.out" redirection and it will hang.
  • Remove the second >"foo0.out" redirection and it will complete.

I also validated the fact that set -m obviated the need for a new process group by adding the following to memusg before the exec bash -i -c "$cmd" call, then watched when it appeared (bash) and when it didn't (bash -m or set -m):


What is the unit of memusg output, bit or byte? Thanks.

really cool script, thanks!

Thanks for the cool tool! :-)

tamlyn commented Sep 14, 2017

@bbsunchen it appears to be using the value of the RSS column from ps which, according to the man page, is measured "in 1024 byte units". But that can't be right as I'm getting usage values like 138487884 and I don't, sadly, have 138 GB or RAM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment