Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Nested Calls, Recursion and MEMORY

by chromatic (Archbishop)
on Dec 19, 2002 at 21:01 UTC ( [id://221255]=note: print w/replies, xml ) Need Help??


in reply to Nested Calls, Recursion and MEMORY

Your test code looks broken: you're only calling two functions in the recurse1() case.

Even after you fix it, it still won't use as much memory as the recursive case. That's the nature of recursion; every time you make another function call, Perl has to save another stack frame. When you return, Perl pops that frame off the stack. (This is a bit over simplified, but think of it like setting aside a task in memory for a moment to do another task. When the second task is done, you return to the first.)

I wouldn't classify this as a memory leak, because it appears that Perl is doing exactly as it should do.

Replies are listed 'Best First'.
Re: Re: Nested Calls, Recursion and MEMORY
by robdog (Novice) on Dec 19, 2002 at 22:31 UTC
    Thanks for your reply.

    The test code is not broken though. The "recurse" function is really recursive, and goes down 2 levels deep. The "recurse1" actually calls "recurse2". This just a 2 level deep nested call. Which pretty much does the same thing as "recurse".

    calling recurse, leaves blocks..
    calling recurse1, leaves no blocks..

    In both examples, they should be saving the same # of call stacks. They essentially do the same thing, they both do a 2 level deep nested call, except that "recurse" calls itself. That is basically the only difference between the two.

    so..to rephrase what u said,
    Everytime i make another function call (either to myself or another function), Perl has to save another stack frame. When you return (either from myself or another function), Perl pops that frame off the stack.

    What im getting at, is that calling yourself or another function in a nested fashion _should_ do the same thing.

    but doesnt appear so.

    thanks for taking the time to read and answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found