Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: memory usage and leakage

by Sewi (Friar)
on Jun 23, 2011 at 15:14 UTC ( #911099=note: print w/replies, xml ) Need Help??

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

You're right as long as the threads do different things.

But he said, he'd need many instances, my suggestion was to make every "instance" one "thread".

Replies are listed 'Best First'.
Re^4: memory usage and leakage
by BrowserUk (Pope) on Jun 23, 2011 at 15:58 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://911099]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-07-16 09:22 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (333 votes). Check out past polls.