[OSM-dev] Monitor memory leaks

Lars Aronsson lars at aronsson.se
Tue May 8 07:50:45 BST 2007

I was told there was one server process eating all available RAM.
Once when I had a similar problem, I used the following code in 
Perl to monitor the process' memory usage:

sub memsize {
    local $/ = undef;   # Read complete files
    open(FILE, "</proc/$$/stat") or return -1;
    my $stat = <FILE>;
    close FILE;
    my @arr = split(' ', $stat);
    return $arr[22];

Here $$ is Perl's variable for the process ID and under Linux 
/proc/$$/stat is where you can find information about the running 
process.  I'm sure this can be translated to Ruby.  A function 
like this can be quite useful, e.g.

 if (memsize() > 10000000) {
    log("Process $$ is >10 meg while doing: " . self->request);

Hope this helps.

  Lars Aronsson (lars at aronsson.se)
  Aronsson Datateknik - http://aronsson.se

More information about the dev mailing list