0

gurus.

I have a perl script which needs to run in an infinite loop and monitor my gnu/linux's load average and free ram and send an email using google's account if the load/ram is above/below the value set.

i found these

Memory usage of Perl script

How can I find memory leaks in long-running Perl program?

and have done as suggested to use undef but that doesn't seems to help as most likely I have something that never gets cleared/freed.

here's a sample of how the memory usage is constantly increasing and after a day it can allocate quite much ram.

Here's the perl script itself. Can someone point me somewhere on how to fix this. Thanks.

Community
  • 1
  • 1
ViruSzZ
  • 3
  • 1

2 Answers2

1

The leak could be hiding in XS part of the modules. It seems like there's no leaking on Perl part. You can try to remove external modules one by one and check when leaking stops.

Anyway, it looks small enough and doesn't have any persistent data between iterations. Why not put it on cron to run every minute instead of keeping it in memory at all times?

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • Yes, I agree using cron is the best solution but I'm just playing there with the script & was not sure if the memory leakage might be coming from the perl part .... I'm using Sys::Statistics::Linux; Net::SMTP::SSL; so will try disabling and see if this is caused by any of them ... thanks – ViruSzZ Jun 21 '12 at 11:39
1

Apart from using cron, there are plenty of system monitoring tools that do all this sort of stuff for you.

Anyway - it'll be easier to check where memory is being used if you write some subroutines along the lines of:

while (1) {
  my $cpu = ...
  my $stat = ...
  if (need_to_send_msg($cpu, $stat)) { send_message($cpu,$stat) }
}

sub send_message {
  my ($cpu, $stat) = @_;
  my $msg_body = compose_message($cpu, $stat);
  transmit_message($msg_body, $from, $to);
}

That will remove a whole bunch of variables from the main body of your script and let you comment out parts of the subroutines to see where memory is used.

Oh - it looks to me as though you are constantly creating $smtp connections even if you aren't going to send a message. It'd be easier to be sure once you've got the main loop smaller by using some subroutines.

Richard Huxton
  • 21,516
  • 3
  • 39
  • 51
  • +1 for the smtp connection. I noticed that, too. @ViruSzZ: That takes up time, memory, and creates a connection in each iteration. Why not create it only if you need it, subroutine or not? – simbabque Jun 21 '12 at 14:32
  • Yes, you were right. I made a rewrite and now it seems the script is not allocating more than 9mb of ram. here's how the rewrite looks like => http://pastebin.com/1JJ50btn and the relevant mem usage => http://pastebin.com/fVTs1pzj are there any tips I can do in order to decrease the memory usage of the script? – ViruSzZ Jun 21 '12 at 17:12
  • That's 1.7% of your RAM and holding steady - even if you chop another 25% off its usage you'll never notice. I would add some more subroutines though - it'll make it easier to see what the main loop is doing and easier to change the various parts of the script. – Richard Huxton Jun 21 '12 at 20:33