Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Nested Calls, Recursion and MEMORY

by MarkM (Curate)
on Dec 20, 2002 at 05:15 UTC ( #221370=note: print w/ replies, xml ) Need Help??


in reply to Nested Calls, Recursion and MEMORY

I would not consider your scenario to show that Perl is 'leaking' memory. Rather, Perl is allocating a bit more memory in a case that you expect should allocate the same or less memory.

Perl requires store to hold lexical variables. For functions that are called many times, but never recursively, Perl can reuse the same lexical variable storage ('pad') for each invocation. When a function is called recursively, new lexical variable storage needs to be allocated, because the previously available storage is currently in use.

The memory is not lost or 'leaked'. Simply, Perl doesn't have an appropriately sized memory block in its freelist, so it allocates a new one.

If you wanted to prove that the memory was actually leaked, you would need to show that multiple iterations of the recurse() function lost additional memory at each iteration. I do not believe that you will find this.


Comment on Re: Nested Calls, Recursion and MEMORY
Re: Re: Nested Calls, Recursion and MEMORY
by Anonymous Monk on Dec 20, 2002 at 20:15 UTC
    Thank you MarkM, for your insights into the memory management of Perl. Very very very helpful.

    So to recap what i have discovered..

    *) Perl DOES do something different when a function calls itself, vs calling another function. If a function which is currently invoked, gets invoked by another or itself, it will allocate more memory, due to the fact that the current variables are already in use.
    I didnt exhibit the extra memory allocations in my Non recursive nested calls, because no functions were ever invoked on themselves. Thus no need to allocate more memory.
    It is not like C, where the functions local variables are pushed onto a stack, and it doest matter if it calls itself or another function. It would all be pushed and popped off correctly.

    As my test code above demonstrated, running the "leaky" recurse function in a loop, does not accumulate more memory. It is reused. So , technically, its not a memory leak.

    In my certain situation, where at the START of my program i do a deeply nested file glob, storing all filenames, then NEVER CALLING that function again, i end up with a program which runs on less memory, and the possibility of virtual memory disk swaps is great.

    To get around this, i will create a seperate perl script which file globs to a text file. I will call this script via a system command, and load the text file it created. That way i have no recursion at the start of my main program, more memory and less disk swaps because of virtual memory.

    So to continue my education, does anyone know or can point me to the sourcefile or document which technically explains how PERL deals with memory, global vars, local vars, etc? What technically happens when you do a local() or a my? I know C, C++/, many flavors of asm, so the more low level the better.

    thank you again
    rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://221370]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-08-22 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls