I can't seem to find where all the memory is going with this script. It's not doing anything fancy in terms of coding and I'm even using undef when I'm done with variables. This script seems to end within hours of starting with "Out of Memory!" errors.
#! /usr/bin/perl -X use threads; $thr1 = threads->new(\&manager); while (1){sleep(1);} sub initiator(){ my @in_items = @_; my $torrent = substr(@in_items[0],2); chomp($torrent); my $f = substr(@in_items[1],2); chomp($f); my $lc = substr(@in_items[2],2); chomp($lc); my $priority = substr(@in_items[3],2); chomp($priority); my $buffer = substr(@in_items[4],2); chomp($buffer); my $saveas = substr(@in_items[5],2); chomp($saveas); my $a,$b,$tr_percent,$tr_time,$tr_down,$tr_up,$tr_err,$pid,$urgent +; $final = "downloader.pl $buffer $saveas $priority $f"; $status = open(PIPE,"python btdownloadheadless.py $final |") || pr +int "Couldn't initiate download"; while (<PIPE>){ #print $_; #open(STANDARD, ">>./$buffer/DEBUG.dat"); print STANDARD $_; c +lose(STANDARD); $ln = $_; chomp($ln); ($vb,$vn) = split(/\>\>\>/,$ln); ($a,$b) = split(/---> /,$_); chomp($b); if ($vn ne ""){ $pid = $vn; open(STANDARD2, ">>./$buffer/PID.d +at"); print STANDARD2 "$pid"; close(STANDARD2); } if ($a =~ "percent done"){$tr_percent = $b;} if ($a =~ "time left"){$tr_time = $b;} if ($a =~ "download rate"){$tr_down = $b;} if ($a =~ "upload rate"){$tr_up = $b;} if ($a =~ "ERROR:"){$tr_err = $_; $tr_time = "complete!";} if ($tr_time eq ""){$tr_time = "Connecting...";} open(OUT, ">>./$buffer/WATCH.dat"); print OUT "$tr_time ( % $tr_percent ) \@ $tr_down $tr_err\n"; close(OUT); if (($tr_time =~ "complete!") || ($tr_time eq "Download Succee +ded!")){ sleep(1); print "\nTrying to end process [$buffer] ($pid)...\n"; system("kill -s KILL $pid"); } $v++; } $urgent = 0; open(STANDARD, ">>./$buffer/DEBUG.dat"); print STANDARD "PIPE ENDED..."; $err = $? & 255; print STANDARD "With: $err"; print STANDARD "With: $?"; print STANDARD "With: $status"; close(PIPE); close(STANDARD); undef $a,$b,$tr_percent,$tr_time,$tr_down,$tr_up,$tr_err,$pid,$urg +ent,$v,$err,$vb,$vn,$ln; undef $torrent,$lc,$f,$priority,$buffer,$saveas,$cmd,$cancel; undef @in_items; } sub manager(){ $|++; print "."; opendir(DIR,"./acc"); @list = readdir(DIR); close(DIR); foreach (@list){ if ($_ =~ ".dat"){ @new_list[$q] = $_; $q++; }} #print @new_list; foreach (@new_list){ open(READER,"./acc/$_"); @items = <READER>; close(READER); #print @items; $thr = threads->new(\&initiator, @items); $line = $_; chomp($line); #cp("./acc/$line","./$line/FORKED.dat"); unlink("./acc/$line"); } undef @list; undef @new_list; undef @items; undef $line; $q = 0; sleep(2); &manager(); }

In reply to Oh where has my memory gone? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.