Dear Monks, This problem has been my headache for two weeks. Can anyone help me? Thanks a lot!
use threads; use SOCKETSVR::Server; my $port = 5188; my $d = new SOCKETSVR::Server LocalPort => $port; print "Socket Server started. port=$port\r\n"; while (my $conn = $d->accept) { # $conn is socket client handler, which is GLOBtype threads->new(\&start_process, $conn)->detach(); undef($conn); } sub start_process { my $conn=shift; if(my $r = $conn->recv_data) { #blabla.... } $conn->close; return; }

This program will create a thread for any socket client connection. But I found the memory is increasing rapidly, and the program will crash after have run for about 3 days.

In the past weeks, I tried a lot of solutions for this problem:

1. I updated Perl from v5.8.8 to v5.10.1, and threads from v1.0.7 to v1.7.5 ----It didn't work.

2. I used Thread::Pool module to avoid massive threads open and close, which might have caused memory leak. ---It prompted error: Can't store GLOB items at lib/Storable.pm. That's because $conn above is GLOB value

3. I tried to transfer fileno($conn) between threads and re-construct socket client by  open my $conn, "+<&=$fno"; ---It didn't work. fileno($conn) always returns 9 and socket client can't be re-opened.

4. I used some other threadpool module ---They didn't work for $conn cannot be shared. it's not a simple data structure.

So after so many tries, I still cannot make it. Can any Monk help me? Thanks!


In reply to Perl threads memory leak with socket and solutions I tried... by ilxl

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.