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"


In reply to threads and RAM-Usage by strat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.