Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: memory usage and leakage

by BrowserUk (Pope)
on Jun 23, 2011 at 15:58 UTC ( #911104=note: print w/replies, xml ) Need Help??


in reply to Re^3: memory usage and leakage
in thread memory usage and leakage

But he said, he'd need many instances,

He actually said:

I'll need to run several instances of this process

And in general, it is a Bad Idea to use threads as (direct replacements for) processes. He would almost certainly save memory by using fork assuming that he's running on a platform with native fork and COW. The savings won't be as dramatic as many people expect, but depending upon the nature and detail of the application, they could be significant relative to running multiple instances of the application within threads.

But the biggest saving would likely come from restructuring the application to make best use of threading. This generally means only threading those small parts of the application that really benefit from concurrency, rather than throwing huge lumps or whole applications into each thread.

For example, people have a tendency to want to run objects across threads. That is, they create objects in their main application, and then when they need the thread to do something that will take a long time and they don't want to wait, their instinct is to pass the object to a thread to do it. Whilst this can be made to work, and can even be convenient for one-off's, it requires a lot of duplication (of memory) and tends to lead to the use of large numbers of short-lived threads, which is expensive.

A better approach is to move the threading inside the class. So, instead of passing a single instance of a MyClass() object to a new thread every time it need to perform the longRunning() method, the class creates a single thread with input and output queues to perform the _longRunning() procedure, and the longRunning() method simple queues the relevant instance data to that thread and immediately returns a promise to the caller. When the _longRunning() procedure has finished processing the instance, it places the results on the output queue. And when the caller asks for the promise to be fulfilled, the result is read from the queue and returned. The caller can also poll the promise periodically to check if it is ready to be fulfilled.

Though this strategy is more complex to program, it advantages are myriad.


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://911104]
help
Chatterbox?
[atcroft]: Discipulus: I looked back at your note about my Padre question from a while back, and it made me look again... as a result, I found what I was missing and was able to make the change. Thank you!

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-02-24 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (311 votes). Check out past polls.

    Notices?