Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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.

Replies are listed 'Best First'.
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://221370]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-02-23 02:54 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.