http://qs1969.pair.com?node_id=1122762

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

I'm puzzled by something that seems simple, but yet is yielding strange results. I'm using threading as part of a decent sized program, the easiest way to describe the purpose of this code is it's part of an alarm system.

What I'm running into is a memory leak when using ithreads, sleep, and detach. This doesn't seem to happen if I use join to wait for the thread to finish execution, but I can't do this because the main loop has to keep running.

Here is a short snippet of code that exhibits the issue, the modules are in there for two reasons, one being it shows the leak better, two being these are what are used in the production code.

use warnings; use IO::Handle; use threads; use strict; use DBI; use Email::MIME; use Email::Sender::Simple qw(sendmail); while (<STDIN>) { chomp(my $msg = $_); print "Received: $msg\n"; if ($msg =~ /start/) { my $thread = threads->create(\&thread); } } sub thread { print "Thread started!\n"; sleep 5; threads->detach(); }

I'm using PERL 5.20.2, compiled using perlbrew. I'm using top and ps to watch memory usage on a Linux server. Type "start" a few times and the memory usage balloons about 6MB each time. At random points the ballooning stops, and PERL will reclaim seemingly about 1MB of memory, then start ballooning again.

What am I missing here?