Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Truer words have never been spoken than what BrowserUK just so-well said:   the key is to limit the number of threads that are active at any one time, and to separate this from the number of requests amount of work that they have to do.   Even if you receive (say ...) 1,000 start messages in the space of (say ...) one millisecond, you must not attempt to “therefore ...” launch 1,000 threads.   Instead, some of those requests will briefly have to wait-their-turn.

You should have a pool of “worker threads,” of some configurable size that will not exceed this system’s capacity.   All of those threads, however many there may be, should be waiting for a message (start ...) to arrive on a common, thread-safe queue.     Meanwhile, your main thread, instead of “starting a new thread” each time such a message arrives, should instead be posting the message to that queue.   When such a message arrives for them, they carry out the work, then wait again.   (The main-thread is the only “writer.”   All of the workers are “readers.”)   If any worker is waiting, it will get the message without delay.   If all of the workers are busy, the message will briefly sit in the queue until some worker can receive it.   Nevertheless, the total number of threads that the operating system is being asked to support ... or, to provide memory for ... will never grow beyond the proscribed limits, no matter how stuffed-up the queue (momentarily) gets.

And, there’s already a lot of CPAN code out there to help you, so be sure not to re-invent the wheel here.   (Say ...) Thread::Pool, Thread::Queue, Thread::Signal . . .

You should, of course, design the threads so that they dispose of all storage needed to service their latest request, before they go-to-sleep waiting for another request to arrive.   If you take care to do that, Perl’s very-clever memory manager should automagically take care of the rest.   Yes, the memory-size of the parent process may grow to be large, but it should not grow uncontrollably leak.

In reply to Re: ithreads memory leak by sundialsvc4
in thread ithreads memory leak by DNAb

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2020-11-28 06:05 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found