in reply to Perl detached threads, loops & my?
Which OS and versionof Perl are you using? This doesn't seem to leak (with 5.10.1 & Vista):
perl -Mthreads -E"sub x{sleep rand 100}; for(1..100){ threads->new( \& +x, $_ )->detach }; sleep 1000"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl detached threads, loops & my?
by expresspotato (Beadle) on Jun 09, 2010 at 00:00 UTC | |
I know the above statement is the WRONG way of using threads but its the only way I know of ensuring a fresh thread each time. It will use all system memory in a matter of seconds. With the following code the program will eventually crash after being run for extended periods (HOURS) of time with other running threads. No error message is observed.
| [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Jun 09, 2010 at 00:19 UTC | |
I'm sorry, but you have an unconstrained flat out loop creating hundreds of thousands of threads ... and you wonder why it runs out of memory? Why are you storing the thread handles when you're detaching them? Did you not see me ask which version of Perl you are using? ... but its the only way I know of ensuring a fresh thread each time. What on earth do you mean by a "fresh thread"? And why are you so certain that you need them? 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.
| [reply] |
by rjt (Curate) on Jun 09, 2010 at 00:27 UTC | |
Thank you for the example. However, I still have doubts. Please carefully consider this reply, my other reply, and BrowserUk's comments. Red flag for me: Your second script, you claim takes "HOURS" to run. On the below mentioned old system, that script completes for me in minutes. Process memory usage never went above 50MB.
You're trying to create 100000 threads. You invite a race condition where your script may hit a wall very quickly if your threads do anything non-trivial. You would expect to see:
Your first example will "leak" memory in the sense that the @thr_ll array will grow to 100000, even after threads exit. On most systems this wouldn't be a big deal if your real application caps at 100000 as well, but I still wouldn't recommend it. I ran both of your scripts on my oldest machine, and neither one had a deleterious effect on Perl 5.10.1 on Ubuntu, running on an 8 year old P4-1.6GHz machine with 512MB. Both ran to completion. | [reply] [d/l] [select] |
by llancet (Friar) on Jun 09, 2010 at 01:13 UTC | |
with a somehow new version perl and threads: Actually, if I do something like your second example, it would core dump immediately:
| [reply] [d/l] [select] |