Question

The memory consumption of the following code increases in the course of its execution. What could be going wrong ? Is there something else I need to do to exit cleanly from the thread ?

#!/usr/bin/perl -w
use strict;    

my  ($i,$URL);
my  @Thread;
my  $NUM_THREADS=4;
my  @response:shared =();
while(1)
{
    for($i=0;$i<$NUM_THREADS;$i++)
    {
         if( $response[$i] is processed)
         {
              $URL=FindNextURL();  
              $Thread[$i]=new threads \&Get,$i,$URL;
              $Thread[$i]->detach();
         }
    }
    # wait for atleast one $response[$i]
    # if ready process it               
}

sub Get
{
    my $i=$_[0];
    my $URL=$_[1];        
    $response[$i]=FetchURL($URL);
    return;
}
Was it helpful?

Solution

from http://perldoc.perl.org/threads.html

"On most systems, frequent and continual creation and destruction of threads can lead to ever-increasing growth in the memory footprint of the Perl interpreter. While it is simple to just launch threads and then ->join() or ->detach() them, for long-lived applications, it is better to maintain a pool of threads, and to reuse them for the work needed, using queues to notify threads of pending work. The CPAN distribution of this module contains a simple example (examples/pool_reuse.pl) illustrating the creation, use and monitoring of a pool of reusable threads."

Please try to have a poll of threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top