Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Gregorian Garbage Collection...

by edwar64896 (Novice)
on Oct 12, 2005 at 11:54 UTC ( [id://499454]=perlquestion: print w/replies, xml ) Need Help??

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

Hi brothers in Perl...

Have possibly fallen into newbie-trap, but anyway...

I am writing a TCP server in perl using threads. When a connection comes in to the server, a couple of threads get spawned. Since starting this effort, I have learned about Perl's propensity for being a little memory hungry, although having said that I am not too worried about it. I am more worried about the fact that memory does not get released when a thread terminates. I have noticed this consistently with async {}; blocks. I understood that when a thread ends, and effectively 'auto-detaches' itself, then the garbage collector should clean everything up relating to that thread. Unfortunately it doesn't seem to happen, and the memory footprint of my poor server just keeps on growing!

Anyone got any idea how I can force the GC to clean up my threads properly? Am using 5.8.5 on FC3

Forgive me brothers, I appear to have sinned...

cheers,

Mark

Replies are listed 'Best First'.
Re: Gregorian Garbage Collection...
by Perl Mouse (Chaplain) on Oct 12, 2005 at 12:10 UTC
    Anyone got any idea how I can force the GC to clean up my threads properly?
    Send in patches!

    I am only half kidding. Or maybe not at all. While there might be all kinds of problems with your code (or not, but there's nothing to inspect), Perl threads have all kinds of problems. Memory leaks belong to those. I do suggest you upgrade from 5.8.5 to 5.8.7. While it seems trivial, it's just two maintenance releases from 5.8.5 to 5.8.7, many of the changes between 5.8.5 and 5.8.7 have to do with threads.

    And to answer your question, you cannot tickle the garbage collector from within Perl - other than reducing the reference count of your values to 0.

    Perl --((8:>*
      Perl threads have all kinds of problems. Memory leaks belong to those... many of the changes between 5.8.5 and 5.8.7 have to do with threads

      Do you have some pointers?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        I guess all the changes are in the Changes files coming with Perl. However, I based my remarks on Allison Randall's "State of the Carrot".
        Perl --((8:>*
Re: Gregorian Garbage Collection...
by TomDLux (Vicar) on Oct 12, 2005 at 14:42 UTC

    Don't forget that garbage collection makes memory available for re-use by the program, but does not (generally) release memory to the operating system.

    Releasing memory to the OS is a complicated matter that most applications/languages/frameworks do not attempt, and is not possible on many OSes.

    In any case, if you have a long-running program that uses and releases memory objects, it would be a pointless expense to release memory to the OS, since you will later allocate new objects, which requires requesting more memory. Only in the case of requiring large amounts of memory during an early stage, followed by a lengthy low-memory-requirement phase, would it make sense. I suppose in such a case you could fork a separate process with the heavy memory requirements, so that it will terminate and release memory, after sending the required data to the main process.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Gregorian Garbage Collection...
by nicholasrperez (Monk) on Oct 12, 2005 at 15:15 UTC
    Have you considering using POE for your TCP server solution? I don't know exactly what it is you are doing, but I am guessing it is some sort of worker thread model. In this case, it is trivial to write a POE-based TCP server to handle quite a few connections "concurrently" depending whether you have long running subs or not. Using a single interpreter will dramatically decrease your memory footprint.

    I can't get you a link at the moment (since draconian filters at work block just about everything), but if you go to the POE wiki and search for evolution of a TCP server, you will find a nice broken down example of going from a simple select based loop all the way to a just a few simple lines of perl using POE::Component::TCP::Server.

    If you are concerned with performance you should know that I have written a webserver serving dynamic pages pulling information from a database all using pre-built POE components, and was managing about 45-50 requests a second with streaming connections as well on a silly little Duron 800Mhz with 256MB of RAM. POE is quite efficient.

Re: Gregorian Garbage Collection...
by zentara (Cardinal) on Oct 12, 2005 at 15:49 UTC
    Here's my 2 cents......

    Reuse your threads, and undef as much as possible within the thread, when the thread run finishes. This way, your memory use should level out eventually, as each thread reuses it's own memory space. Repeatedly running threads async, is similar to that create-destroy cycle which tends to gain memory in OO programs. Reusing objects fixes it.

    Without testing, I would say that each threads memory use will be the maximum of it's most memory-intensive run. But if you undef everything, then on it's next run, it should reuse it's memory.


    I'm not really a human, but I play one on earth. flash japh
Re: Gregorian Garbage Collection...
by fmerges (Chaplain) on Oct 13, 2005 at 01:27 UTC

    Hi,

    One thing, the memory used get freed to the interpreter not to the system. Update: this was also said by TomDLux

    As nicholasperez said, I think is a better approach to use POE, because, states machines concept runs well, threads in perl are still...

    But I think that we could give you a better answer if you show us what you wanna do...

    Regards,

    |fire| at irc.freenode.net
    fmerges at irc.perl.org
Re: Gregorian Garbage Collection...
by BrowserUk (Patriarch) on Oct 12, 2005 at 18:42 UTC

    I don't suppose you'd care to show us your code?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://499454]
Approved by jkva
Front-paged by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2025-07-19 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.