Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer: Determining memory usage of a process...

by rob_au (Abbot)
on Sep 28, 2001 at 16:22 UTC ( #115380=categorized answer: print w/replies, xml ) Need Help??

Q&A > programs and processes > Determining memory usage of a process... - Answer contributed by rob_au

The best method to do this would be to make use of the Proc::ProcessTable module which provides access to the Unix process table in a consistent fashion, hiding the vagarities of different /proc implementations.

The following documented code will return the total memory usage and the percentage memory utilisation of the current process by iteration through the process table:

#!/usr/bin/perl -w use strict; 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; }

 

Ooohhh, Rob no beer function well without!

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Corion]: marto: I think the first thing sleep deprivation does is kill your sense of how tired you are :)
[marto]: I think that actually caught up with me yesterday, felt a really sharp pain where my neck meets my skull.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-11-19 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (280 votes). Check out past polls.

    Notices?