A detached thread won't release its memory until the main code exits out.

Um. That's simply not true. Detached threads release their memory as soon as they terminate.

This is clearly demonstrated here. I start 100 threads that sleep for 3 seconds. I wait a second to ensure they are all running and then check the process memory usage, which stands at 59MB.

I then wait another 5 seconds to ensure they all end, and check memory again. It's now 25MB.

#! perl -slw use strict; use threads; sub checkmem { print qx[ tasklist /nh /fi "pid eq $$"]; } async(sub{ sleep 3 })->detach for 1 .. 100; sleep 1; checkmem; sleep 5; checkmem; __END__ c:\test>junk74 perl.exe 4928 Console 1 58 +,960 K perl.exe 4928 Console 1 24 +,696 K

Not all the memory is released back to the OS--indeed, on some systems maybe none of it will be--but it has been released back to the process memory pool for re-use.

Non-detached threads don't release their memory until joined, but detached thread release it as soon as they exit the thread sub.

If you think you have code where this doesn't happen, please post it and we can work out what you're doing wrong.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^2: using the thread->kill() feature on linux by BrowserUk
in thread using the thread->kill() feature on linux by zentara

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.