in reply to Re: Perl, C and deallocation responsibility
in thread Perl, C and deallocation responsibility

> Of course, Perl will never release any memory back to the O/S, but it will reuse it for its own purposes!

This is not correct. It depends on the implementation of the user-level malloc function. For more detail, see:

which notes that on Linux and Windows at least (unlike most other Perl platforms) the standard C Library malloc function does return "big" chunks of memory back to the OS.

Update: Though it's possible for a Perl built with a suitable malloc function to return memory to the OS, in practice this happens rarely, as described in ikegami's excellent Mini-Tutorial: Perl's Memory Management - which concludes with "You can't rely on memory being returned to the system, but it can happen ... if and when memory is returned to the OS is dependant on the memory allocation mechanism perl was compiled to use ... you are more likely to see memory being released to the OS on Windows".

See also:

References Added Later

Updated: minor improvements to wording; added extra references.

  • Comment on Re^2: Perl, C and deallocation responsibility (Returning Memory back to the OS References)
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Perl, C and deallocation responsibility
by Marshall (Canon) on Apr 30, 2023 at 07:11 UTC
    Thanks for the info!
    I haven't seen memory go back to the O/S, but I haven't had the need to look closely on Win 10. The last time I got really anxious about memory was when developing a Perl application for Win XP. I had a memory budget for that one. Often for Perl applications, I see memory usage approach a limit in an asymptotic way and then just stay flat forever. Of course, mileage varies a lot depending on the application's needs.

    I think in the context of the OP's question, one question is whether anybody is going to do anything to either free or reuse the memory allocated for the char*. I was happy to have found an explicit statement about what is going to happen if the listed memory allocation routine is used. I was and am not at all clear what actually happens with the char* declaration (instead of an SV that Perl will reference count upon). There is obviously some type mangling that happens underneath the covers, but I'm not sure how.

    BTW, I have found that SQLite is very good at returning memory if you adjust its memory usage during run time. Giving an SQL indexing operation a lot of memory can have a huge positive performance effect. And then shrink down memory size for normal usage.