strat has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks,
I am just playing around a bit with perl5.8 threads, and would like to write a webserver that can handle many connections under Win2k (well, I know there are OS limits). So I came up with the following code (reduced to the essentials for test purposes):#! /usr/bin/perl use warnings; use strict; use threads; use threads::shared; use IO::Socket::INET; use IO::Select; use POSIX; use constant SERVERPORT => 8090; use constant CRLF => "\015\012"; use vars qw($Done); $Done = 0; $| = 1; my $socket = IO::Socket::INET->new ( LocalPort => SERVERPORT, Listen => SOMAXCONN, Reuse => 1, ) or die "Error: couldn't create listening socket: $!\n"; my $in = IO::Select->new($socket); print "Listening for connections on port: ", SERVERPORT, "...\n"; while (! $Done) { next unless $in->can_read(); next unless my $conn = $socket->accept; threads->new(\&HandleConnection, $conn); } # while warn "Normal termination\n"; # ======================================================= sub HandleConnection { my $conn = shift; my $thread = threads->self; $conn->send("<html><head></head><body>"); $conn->send("<h1>Testpage</h1>"); $conn->send("</body></html>"); $conn->close(); print "Connection: ", $thread->tid, " finished------\n"; } # HandleConnection
This code works fine (although the http-handling is very dirty), but it seems to have a big memory leak. At the beginning, it uses about 5MB RAM, but with every creation of a new thread, it adds about 2-3 MB RAM (if I put a comment in front of threads->new(\&HandleConnection, $conn); the RAM usage stays constant).
Is there a possibility to free the memory used by a thread with Activestate Perl Built 805, or do I have to use a fixed pool of threads (may become difficult because I'd like to write a chat webserver, e.g. like Poor man's webchat from merlyn, but since HTTP::Deamon is not thread-safe, I'd like to build my own HTTP dealing threadsafe module. I really would like to to it with threads to get a better feeling for them; I don't want to use POE because there may be threads that run several hours for outputting data, and I don't want to use fork because of windows, as well as Coro(utines).
Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: threads and RAM-Usage
by BrowserUk (Patriarch) on Jun 14, 2003 at 12:26 UTC | |
|
Re: threads and RAM-Usage
by deadkarma (Monk) on Jun 14, 2003 at 22:13 UTC | |
by BrowserUk (Patriarch) on Jun 14, 2003 at 22:44 UTC | |
by deadkarma (Monk) on Jun 14, 2003 at 23:02 UTC | |
by BrowserUk (Patriarch) on Jun 15, 2003 at 01:46 UTC | |
by deadkarma (Monk) on Jun 15, 2003 at 17:03 UTC | |
|