Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to run a memory-consuming program and track its memory usage over time. The program ends in a few seconds after invoked.

This previous question suggested sysstat package . While its pidstat utility partly does what I want, it fails to meet my 2 needs:

  • It accepts a minimal interval of 1s, but I want to measure at shorter granularity. (0.1s should be fine)
  • It only tracks an existing process, while I cannot always be present to copy&paste the pid.

Are there some alternative scripts / utilities to do the invoke-and-measure job better?


share|improve this question
Sounds like you should write yourself up a simple little Python or Bash script to dump the process' memory usage (you can just view the first integer in /proc/$PID/statm), and then sleep for 100ms and repeat. Why can't you just keep throwing the PID's associated statm through cat, maybe use some regex to filter off the extra/unneeded values, and just do a sleep 0.01? Some operating systems won't allow sub-second sleep values, so in that case, you'd have to take the Python route (and use Python's built-in time library to sleep instead). – Breakthrough Jul 15 '13 at 14:55
there are many ways – munish May 6 at 5:11
up vote 9 down vote accepted

This should do what you need. It gets the information from /proc/$PID/stam and prints (from man stat):

              size       total program size
                         (same as VmSize in /proc/[pid]/status)
              resident   resident set size
                         (same as VmRSS in /proc/[pid]/status)
              share      shared pages (from shared mappings)
              data       data + stack

The script:

#!/usr/bin/env bash 

## Print header
 echo -e "Size\tResid.\tShared\tData\t%"
 while [ 1 ]; do
    ## Get the PID of the process name given as argument 1
     pidno=`pgrep $1`
    ## If the process is running, print the memory usage
     if [ -e /proc/$pidno/statm ]; then
     ## Get the memory info
      m=`awk '{OFS="\t";print $1,$2,$3,$6}' /proc/$pidno/statm`
     ## Get the memory percentage
      perc=`top -bd .10 -p $pidno -n 1  | grep $pidno | gawk '{print \$10}'`
     ## print the results
      echo -e "$m\t$perc";
    ## If the process is not running
      echo "$1 is not running";

You can then call the script, giving it a process name as input. For example:

$ firefox
Size    Resid.  Shared  Data    %
517193  261902  9546    400715  12.8
517193  261902  9546    400715  12.8
517193  261902  9546    400715  12.8
517193  262100  9546    400715  12.8
517193  262100  9546    400715  12.8
517193  262100  9546    400715  12.8
517209  261899  9546    400731  12.8
517209  261899  9546    400731  12.8


  • This assumes that there is only a single running process with the name specified.
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .