The following code should always run a maximum of 20 threads processing some data. Running the code while looking at the memory consumption with top shows that the perl process keeps using more and more memory.
I am using perl 5.12.2 with threads 1.75 under FreeBSD 8.1 (64bit).
I am not sure where the memory leaks. Am I missing something?
#!/usr/bin/perl
$|=1;
use strict;
use warnings;
use threads;
use threads::shared;
use Data::Dumper;
# Global vars
# Maximum working threads
my $MAX_THREADS = 20;
# Flag to inform all threads that application is terminating
my $TERM:shared=0;
# Prevents double detach attempts
my $DETACHING:shared;
# Signal handling
$SIG{'INT'} = $SIG{'TERM'} = sub {
print("^C captured\n");
$TERM=1;
};
# thread
sub stuff_thr($) {
my ($job)=@_;
# My thread ID
my $tid=threads->tid();
# do some thread stuff
print "Hi, I am thread: $tid, I need to do something with $job\n";
# Detach and terminate
{
lock($DETACHING);
threads->detach() if ! threads->is_detached();
}
return(0);
}
# main
sub main() {
# Manage the thread pool until we run out of data or signalled
# to terminate
my @jobs=(1..100);
while (@jobs && ! $TERM) {
# Keep max threads running
for (my $needed = $MAX_THREADS - threads->list();
$needed && ! $TERM; $needed--) {
my $job = shift(@jobs);
last if (! $job);
# New thread
threads->create('stuff_thr',$job);
}
# normally fetch a limited amount of data from a db to
# process, at this point just make sure the job queue
# is never empty
if(scalar(@jobs) < 10) {
@jobs=(1..100);
}
}
while ((threads->list() > 0)) {
# waiting for threads to finish
sleep(1);
}
}
# enter main()
main();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|