you say the anonymous sub isn't a closure

Indeed I did. I expressed myself poorly and I've rephrased my post to hopefully better express what I meant.

(But first, to avoid any confusion: a closure is a subroutine, named or anonymous, that binds its lexical context.)

A lexical anonymous recursive subroutine is by necessity a closure. What I meant was that the subroutine, if written as a named subroutine, isn't a closure. By making it lexical and anonymous you make it one though, but the only lexical surrounding it is using is its own declaration.

The memory leak comes from that inside the subroutine, you use $_fibo. That makes $_fibo reference a subroutine that references $_fibo. Thus, $_fibo is a circular reference and refcount will never drop below two and won't be garbage collected.

This below illustrates what I've just said. On my perl, and probably yours too, the following will print the same for each iteration.

sub foo { my $foo; $foo = sub { }; print $foo; } for() for 1 .. 5; __END__ CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78)
This is because $foo gets garbage collected and the memory slot may be is currently is reused. (Since this just happens to be the implementation you can't trust it to always perform the same though.)

If you change the subroutine to bind $foo you get

sub foo { my $foo; $foo = sub { $foo }; print $foo; } for() for 1 .. 5; __END__ CODE(0x274f7c) CODE(0x274da8) CODE(0x1823e48) CODE(0x1823e90) CODE(0x1823ed8)
Here we see that the memory slot isn't being reused.

It can also be interesting to look at the output of the &Dump subroutine in the Devel::Peek package. (For extra effect, dump the sub after doing differently many recursive calls.)

I hope I managed to express myself better this time.

ihb

Read argumentation in its context!


In reply to Re^4: Is it possible to create a sub exclusive to a sub? (nasty memory leak) by ihb
in thread Is it possible to create a sub exclusive to a sub? by punkish

Title:
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, details, 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, summary, 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.