|Pathologically Eclectic Rubbish Lister|
Re^4: memory usage and leakageby BrowserUk (Pope)
|on Jun 23, 2011 at 15:58 UTC||Need Help??|
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.