in reply to Memory leak on definition of anonymous code?

That's rather old news. You have just generated a cyclic reference: the variable references the closure and the closure references the variable. Perl cannot garbage collect such data structures because it uses a simple reference counter to keep track of referenced memory; in the case of cyclic structures all members reference each other, so the counter never reaches zero even if there aren't any references pointing to the structure from the outside. WeakRef may be of interest.

Makeshifts last the longest.

  • Comment on Re: Memory leak on definition of anonymous code?

Replies are listed 'Best First'.
Re: Re: Memory leak on definition of anonymous code?
by tilly (Archbishop) on Oct 07, 2003 at 06:52 UTC
    For the curious, here is the leaky code fixed with WeakRef:
    use strict; use WeakRef; while(1) { &leak(); } sub leak { my $sub_ref; $sub_ref = sub { &$sub_ref(); }; weaken($sub_ref); # <- This fixes the leak return 0; }