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?
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?
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
.
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";
Use GTop
if you want something slightly more portable. The mod_perl
manual has usage examples.