Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I wrote a simple recursive routine which file globs a directory, saves all the entires in an array, and recursivly does the same for any sub dirs.. i noticed that a "TON" of memory was consumed, during the process.. and apon return , using Devel::Leak, many many blocks were still allocated..
Calling the same function again, did NOT accumulate any more blocks.. Ok, maybe Perl is reusing the memory.. fine.
So i thought, a recursive function, is just a nested call onto "ITSELF". Would the same memory consumption apply if Function A called Function B, and Function B called Function C? These are just nested calls, but not onto itself. I got back ZERO blocks allocated after Function A. As i wanted. As _should_ be.
So i assumed that nested calls were OK.. but just not on itself. (due to a memory not being immediately released)..
So Obi Wan, i now ask why. Why is memory allocation different when a nested calling function , calls itself, vs calling another function. Does perl have special code to deal with recursion?

Any and all replies are appreciated...

Below is my test code...

System : Win2k, ActiveState 5.6.1.

use strict; sub cat($) { my $var = shift; print "cat ($var)\n"; } sub bat($) { my $var=shift; print "bat ($var)\n"; cat($var); } sub apple($) { my $hello=shift; print "Apple ($hello)\n"; bat($hello); } sub recurse($) { my $count=shift; print "0Count = ($count)\n"; if ($count >= 1) { undef($count); print "Leaving...\n"; return; } $count++; recurse($count); } sub recurse1($) { my $count=shift; print "Count1 = ($count)\n"; if ($count >= 1) { undef($count); print "Leaving...\n"; return; } $count++; recurse2($count); } sub recurse2($) { my $count=shift; print "Count2 = ($count)\n"; if ($count >= 1) { undef($count); print "Leaving...\n"; return; } $count++; } print "GetAll Test\n"; use Devel::Leak; my $handle; my $count = Devel::Leak::NoteSV($handle); print "Memory Count Start = ($count)\n"; # this loop is here to test accumulation of memory.. # by increasing the loop max, it does not accumulate # more memory usage.. it _seems_ to reuse....the # results do not change.. for (my $xx=0; $xx< 1; $xx++) { recurse(0); # leaky toilet # recurse1(0); # no leak # apple(1); # no leaks } my $count1= Devel::Leak::CheckSV($handle); my $diff = $count1- $count; print "\nCount = ($count1)\n"; print "Difference in Memory = ($diff)\n"; #=== END----

In reply to Nested Calls, Recursion and MEMORY by robdog

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2018-03-20 06:14 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (248 votes). Check out past polls.