#!/usr/bin/perl

# getProcessRss:
# This script gets the 'RSS' of the process specified as a command-line arg.
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using.
# Example of use:  getProcessRss Safari
#
# Cameron Hayne (macdev@hayne.net)  December 2006

use warnings;
use strict;

die "Usage: getProcessRss: name_of_process\n" unless scalar(@ARGV) == 1;
my $procName = $ARGV[0];

my $psCmd = "/bin/ps -xc -o command,rss";

open(PS, "$psCmd |") or die "Can't run command \"$psCmd\": $!\n";
while (<PS>)
{
    if (/^\s*$procName\s+(\d+)\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf("%.1f", $rss / 1024);
        print "$rssMB MB\n";
        last;
    }
}
close(PS);

