Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Re: Nested Calls, Recursion and MEMORY

by Anonymous Monk
on Dec 20, 2002 at 20:15 UTC ( #221503=note: print w/replies, xml ) Need Help??

in reply to Re: Nested Calls, Recursion and MEMORY
in thread Nested Calls, Recursion and MEMORY

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
  • Comment on Re: Re: Nested Calls, Recursion and MEMORY

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://221503]
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: (4)
As of 2018-02-24 22:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (311 votes). Check out past polls.