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

The exact same build.

The memory usage does go up for each recursive call to bar (which is why I had to lower the max value for $i), but it does get reclaimed.

Replies are listed 'Best First'.
Re^4: memory leak when using tail recursion?
by Limbic~Region (Chancellor) on Feb 14, 2007 at 00:14 UTC
    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

      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?