baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi ppl,

well as the title says i need some help with this bug i'm having. or should i say, just an indication of what could be the problem. so i have this program that constantly waits for a std input to do something (regular while loop). depending on an input i runs some procedure and it can do it as many times as user wishes it with constant memory usage, because every time the memory is cleaned:

pseudo code or something just to illustrate what the code is doing use strict; my %hs1; #... $| =1; while(1){ chomp(<>); do something to fill %hs1 if ($_=~/X/); sleep 5; %hs1 = (); }
so the other day my boss comes to me and says: "How do i know that the program didn't froze while i'm waiting for a result, i don't! you have to make this visible somehow...." so i said ok. ( the process that fills the %hs1 is quite long due to extensive calculations it does. (arbitrary precision calculations (with a cutoff) are preformed, so it can take up to some 2-200 min )). since i can't predict how many steps the algorithm is going to do (for some regular progress bar) i decided to trick him by creating an extra thread, detach it, and make a while loop that runs continuously until some thing stops it. in this case the check-point is a finish status(when process is done).(please don't judge me i know that circling around the truth isn't the best way to go, but it's just a quick fix until i get some time to do it properly :) ) :
use strict; use threads; use threads::shared; share(my $var); #some other hashes not shared; my %hs1; #... $| =1; while(1){ chomp(<>); _progress(); do something to fill %hs1 if ($_=~/X/); _stop(); sleep 5; %hs1 = (); } sub _progress { threads->new(\&_status)->detach; } sub _status { my $count = 0; while($var){ my $bar; if ($count == 10){ $bar = '' ; $count = 0; } $bar .= '>' print "\r$bar"; $count++; sleep 1; } } _stop { $var = 0; }
so where is the problem : when i implement the threads, the memory is just getting larger every time i go through the main while loop. maybe this example doesn't illustrate the problem quite accurately but in the real example when i remove the threading everything is ok. my question is : does somebody sees why the threading would cause the memory leakage? (implementation of the threading model is accurately described within the example )

thank you for reading this long post!

UPDATE:

THNX zwon, sadly you found the problem !

Replies are listed 'Best First'.
Re: [threads]memory leak - help needed with debugging
by zwon (Abbot) on Dec 09, 2009 at 20:03 UTC
      In this case I would change the script to use fork. Just send the child process a signal when it's supposed to stop.