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.

In reply to Re^4: memory usage and leakage by BrowserUk
in thread memory usage and leakage by szabgab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.