Re: Memory usage testing
[email protected] (zentara) Sun, 04 Aug 2002 08:40:09 -0400
| Newsgroups | perl.crypto |
|---|---|
| Message-ID | <[email protected]> |
On Sat, 3 Aug 2002 19:50:05 -0400 (EDT), [email protected] (John Francis) wrote: >Hello, > I was wondering if anymone knew a way to keep track of how much >ram was being used by a program. > - John #!/usr/bin/perl -w use strict; #by rob au print join("\n", &memusage), "\n"; exit 0; # memusage subroutine # usage: memusage [processid] # this subroutine takes only one parameter, the process id for # which memory usage information is to be returned. If # undefined, the current process id is assumed. # Returns array of two values, raw process memory size and # percentage memory utilisation, in this order. Returns # undefined if these values cannot be determined. sub memusage { use Proc::ProcessTable; my @results; my $pid = (defined($_[0])) ? $_[0] : $$; my $proc = Proc::ProcessTable->new; my %fields = map { $_ => 1 } $proc->fields; return undef unless exists $fields{'pid'}; foreach (@{$proc->table}) { if ($_->pid eq $pid) { push (@results, $_->size) if exists $fields{'size'}; push (@results, $_->pctmem) if exists $fields{'pctmem'}; }; }; return @results; }