title: Perl - threads join and system running out of memory
body: <p>I posted <a href="https://stackoverflow.com/questions/16348367/array-missing-elements-with-a-subroutine-thread">couple of</a> <a href="https://stackoverflow.com/questions/16326515/perl-create-a-new-array-and-pass-it-to-a-subroutine-thread">question</a> for array and threads in my Perl code. Here is the code again. 
  Based on the suggestions, I made changes and it seemed to be working. Still the code is running into a new issue. The machine is running out of memory. Many threads are finished and unjoined. </p>
  
  <p><strong>Code</strong> - </p>
  
  <pre><code>#!/usr/bin/perl -w -I /opt/hypertable/0.9.7.3/lib/perl -I /opt/hypertable/0.9.7.3/lib/perl/gen-perl
  use strict;
  use IO::Socket;
  use Geo::IP;
  use threads qw(stringify);
  use Net::NBName;
  use Data::Dumper;
  use Hypertable::ThriftClient;
  
  # Syslog Variables and Constants
  my $MAXLEN = 1524;
  my $limit = 5; #for testing
  my $sock;
  my $thr;
  # Start Listening on UDP port 514
  $sock = IO::Socket::INET-&gt;new(LocalPort =&gt; '514', Proto =&gt; 'udp') || die("Socket: $@");
  
  my $buf = '';
  do{
  my $count = 0;
  my @set;
  
  for ($count = 0; $count &lt;= $limit; $count++) {
    $sock-&gt;recv($buf, $MAXLEN);
    my ($port, $ipaddr) = sockaddr_in($sock-&gt;peername);
    my $hn = gethostbyaddr($ipaddr, AF_INET);
    $buf =~ /&lt;(\d+)&gt;(.*?):(.*)/;
    my $msg = $3;
    $set[$count][0] = $hn;
    $set[$count][3] = $msg;
    print $count." --&gt; ".$set[$count][0]." --&gt; ".$set[$count][4]."\n";#Print original array, should print 5 elements 
  
    $thr = threads-&gt;create('logsys',@set);
  
    #&amp;logsys(@set);
  }
  }while(1);
  
  $thr-&gt;join();
  
  sub logsys {
    my $count = 0;
    my @set = @_;
    my $geoip = Geo::IP-&gt;new(GEOIP_CHECK_CACHE);
    my $nb = Net::NBName-&gt;new;
  
    print "--------------------- ".scalar (@set)." -------------------\n";
  
    for ($count=0; $count &lt;= $limit; $count++) {
      print $count." --&gt; ".$set[$count][0]." --&gt; ".$set[$count][5]."\n";#print passed array, should same exact 5 elements
      if (open(WW,"&gt;syslog")){
        print WW $count." --&gt; ".$set[$count][0]." --&gt; ".$set[$count][6]."\n"; close(WW);
      }
    }
  }
  </code></pre>
  
  <p><strong>Error -</strong> </p>
  
  <pre><code>Out of memory!
  Callback called exit at /usr/lib/perl/5.10/IO/Socket.pm line 287.
  Thread 646 terminated abnormally: main=HASH(0xee3b3068) at ./syslogd.pl line 50.
  Perl exited with active threads:
          9 running and unjoined
          644 finished and unjoined
          0 running and detached
  </code></pre>
  
  <p>What does this mean? Why the machine is running out of memory even when many threads are finished? How do I fix this? I can provide more info if needed. </p>