It looks like, memory requested by XS code is eventually returned to OS (right?).

No. Not in general.

Under Windows(*), if you request a large (from memory it was >1MB; but that may have changed) contiguous chunk of memory; then instead of calling the C runtime malloc(), it goes direct to the OS and uses VirtualAlloc() to allocate a distinct chunk of virtual memory from the system memory pool rather than taking a chunk from the process' heap.

Because that single large allocation is only used for that single purpose -- a very large string or the base allocation of a large array or hash -- when the owning Perl entity (scalar/hash/array) is freed, the entire virtual allocation can be returned to the OS.

This includes the stacks allocated to threads which under the defaults, often predominates the memory used by a thread -- see threads module stack_size parameter for how to vastly reduce the memory consumption of threads. Most of my threaded coded uses nothing more than a 4kB stack rather than the default of 1MB.

But all allocations less than that 1MB limit -- which for most Perl programs is the vast majority of allocations -- come from process heap via the CRT malloc(), and they are never returned to the OS until the process ends.

Your program is unusual in that you are apparently using both threads -- which tend to allocate large chunks for stacks -- and pdl with very large piddles, which are allocated as single contiguous chunks. That combination when running on windows gives you a distorted view of Perl's ability to return memory to the OS.

*I seem to recall seeing mention that Perl now(v5.1something) does a similar thing -- bypassing malloc() for large contiguous allocations -- under *nix; but I couldn't swear to it.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^9: ithreads memory leak by BrowserUk
in thread ithreads memory leak by DNAb

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.