views:

123

answers:

3

I want to monitor memory usage when running a program in perl, so that if the memory used by the current program is more than a threshold, I apply approach A, otherwise, apply approach B.

Anyone has any idea?

+1  A: 

This is going to be highly OS specific. For Linux, I was able to find the CPAN module Sys::Statistics::Linux, which is able to read /proc and get you data about your current process. On the off-chance that you're running on Solaris, there's Solaris::Procfs. I couldn't find anything for Windows.

Update: Since you are on Solaris, you definitely want Solaris::Procfs.

JSBangs
Thanks I will try both Solaris::Procfs and Proc::ProcessTableThis is a very good forum where I can learn in a quick way. I like it.
+3  A: 

I found this script from http://www.perlmonks.org/?node_id=235757:

#!/usr/bin/perl  

use Proc::ProcessTable;  

sub memory_usage {  
  my $t = new Proc::ProcessTable;  
  foreach my $got ( @{$t->table} ) {  
    next if not $got->pid eq $$;  
    return $got->size;  
  }  
}  


print 'memory: '. memory_usage()/1024/1024 ."\n";  
You should learn to format the code correctly on SO, but +1 for good effort and good result.
DVK
ROFL... looks like 3 people tried to format the code correctly at the same time :)
DVK
I did use `` to include the code, but it did not work this time. I also tried highlight some words sometimes ago using 4 spaces. did not work either. Thanks all for editing my article. Yes, definitely need to spend some time to learn how to edit in the right way.
+1  A: 

Use GTop if you want something slightly more portable. The mod_perl manual has usage examples.

daxim