in reply to Re^3: memory leak when using tail recursion?
in thread memory leak when using tail recursion?

ikegami,
Very interesting. I am up over 200MB in 30 seconds if I use the block form of if. If I use the post modifier form of if, it stays fixed at 2MB.

Cheers - L~R

  • Comment on Re^4: memory leak when using tail recursion?

Replies are listed 'Best First'.
Re^5: memory leak when using tail recursion?
by ikegami (Patriarch) on Feb 14, 2007 at 00:20 UTC
    The if statememt creates a stack frame, while the statement modifiers do not. goto must reuse the sub's frame when possible. It apparently can't when the sub's frame isn't the topmost frame on the stack.
      That's interesting. The following code doesn't leak though:
      #!/usr/bin/perl use strict; use warnings; bar(); sub bar { my $result = foo(); if ( $result ) { goto &bar; } return; } my $i = 0; sub foo { return $i++ < 1_000_000_000; }
      Wouldn't this leak if the if-statement was creating a stack frame as you said?
        Intersting! I'm even willing to dive into the guts to see if I can find the answer, but I can't tonight.

        I confused "scope" with "stack frame". While if creates a lexical scope, no stack frame is created.

        Because if creates a lexical scope, the following dies:

        >perl -e "use strict; if (my $x = foo()) { } print $x; Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

        If if created a stack frame, the following would print abcde instead of abced:

        sub P1::DESTROY { print 'd'; } sub P2::DESTROY { print 'c'; } { print('a'); if (my $x = bless({}, 'P1')) { my $y = bless({}, 'P2'); print('b'); } print('e'); } print("\n");

        Anyway, I didn't find an answer to your question.