http://www.perlmonks.org?node_id=543356

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

Hi ,
i try to use threads, however, I found my program mem usage grows up from 4 mb to 100 mb .
i try to use detach(), but memory still cannot release. Is that any problem for my program ?

Replies are listed 'Best First'.
Re: threads - memory grow large
by eXile (Priest) on Apr 14, 2006 at 15:11 UTC
    My limited knowledge of perl memory management is that perl never (or hardly ever) free's up memory back to the OS, but that it keeps already allocated memory for itself even if it is not using it.
Re: threads - memory grow large
by zentara (Archbishop) on Apr 14, 2006 at 17:00 UTC
    You need to reuse your threads. It's a misconception that if you use detach, that somehow it all dissppaears after the thread is finished and the memory is reclaimed to the system. It is reclaimed to Perl.

    So what you need to do, is NOT detach, and work out a scheme to reuse your thread threads. I usually put them in a "sleeping condition", and have them periodically check to see if they should wake up and do something. It dosn't use much cpu for a thread to sleep and wake up every 50 milliseconds( or even 1 second).

    You may also be able to use detach , but just reuse the variable name you assign to the thread, but I havn't tested that much. All this is complicated by what you are using in your thread. Try to keep all extra objects contained completely within each thread. But it is all so complex, that you really need to show some code example, then we can help you work out solutions.


    I'm not really a human, but I play one on earth. flash japh
Re: threads - memory grow large
by moklevat (Priest) on Apr 14, 2006 at 16:10 UTC
    What OS are you using? How are you monitoring memory usage?

    You might want to check out this node about memory usage under Linux to see if it helps.

    If it doesn't fit, please post some code and details of your system.

    Update: Fixed broken link. Thanks mpeg4codec.

      Your link there wasn't working properly for me. If you have problems with the link above, try using this link.
Re: threads - memory grow large
by thomas23 (Acolyte) on Apr 14, 2006 at 15:09 UTC
    can you show us some code and test data so we can take a better look at your problem.
Re: threads - memory grow large
by jdhedden (Deacon) on Apr 19, 2006 at 15:14 UTC
    There may be a memory leak in the version of the threads module you are using. Try installing the latest version (1.23) from CPAN.

    Remember: There's always one more bug.
      For people using RedHat EL4 (or below), the default version of threads is 1.05. Any version after (and including) 1.23 will clear these problems out.