in reply to Re^3: How to implement a Queue that doesn't leak memory?
in thread How to implement a Queue that doesn't leak memory?

> Of course, but so could any program doing anything. That doesn't constitute a leak.

maybe I misread "reallocating", if that means that at some point the whole array is moved (copied) to a formerly released memory location it's fine.

If Perl was only allocating forward after x push and releasing backwards after x shift and the released memory wasn't reused otherwise, then memory consumption for the process would grow.

This I'd call a leak.

It would be interesting to know how and when this reallocation happens and if it's automatically done on C-level.

update

I now seem to remember (in the context of paging to the disk) that memory management operates on physical blocks or pages which are dispersed over RAM, and translates a virtual address to a physical one. Hence a released block would be naturally reused, without much intervention. :)

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: How to implement a Queue that doesn't leak memory?
by eyepopslikeamosquito (Archbishop) on Feb 04, 2024 at 02:13 UTC

    > It would be interesting to know how and when this reallocation happens and if it's automatically done on C-level

    I suggest you study your good friend 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: Returning Memory back to the OS References

    👁️🍾👍🦟
Re^5: How to implement a Queue that doesn't leak memory?
by Anonymous Monk on Feb 05, 2024 at 14:19 UTC
    It would be interesting to know how and when this reallocation happens and if it's automatically done on C-level.

    No, nothing is automatic in C. Heck, C barely has arrays but normally you would malloc(3) an intial size and when you need more memory you would use realloc(3) to ask for more. A lot of time realloc can just extend the array but sometimes it needs to allocate more space and memcpy into the new larger space.

    I wish I had the link where I saw it (LWN?) but it turns out that, in practice, Linux can do some magic with pages and you almost never get that stall from having to copy the whole array.

    I don't know what perl does under the covers but it is extremely likely it follows this traditional approach.