in reply to Re: using the thread->kill() feature on linux
in thread using the thread->kill() feature on linux
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.
|
---|