in reply to Re: Memory leaks and circular references
in thread Memory leaks and circular references

Thanks for the short answer. I did just that, and it worked beautifully.

Final code example:
package Top; sub new { my $class = shift; my $s = bless { }, $class; $s->{bottom} = Bottom->new( $s ); return $s; } #===================================== package Bottom; use Scalar::Util 'weaken'; sub new { my ($class, $top) = @_; my $s = bless { top => $top }, $class; weaken($s->{top}); return $s; } #===================================== package main; use strict; use warnings 'all'; use Devel::Cycle; my $top = Top->new(); my $bottom = $top->{bottom} or die "NO BOTTOM!"; $top = $bottom->{top} or die "NO TOP!"; find_cycle( $top ); find_cycle( $bottom );
Expected output: (nothing)

UPDATE: - Now it burns through 52Mb RAM in 26 seconds. Not sure if that's good or bad. I would expect Perl to recycle the RAM. Is that expectation wrong? Sure, 52Mb is better than the original number of 200Mb, but I would like to see no leakage at all.

Is it possible to do this without leaking memory?

Replies are listed 'Best First'.
Re^3: Memory leaks and circular references
by shmem (Chancellor) on Sep 07, 2008 at 00:02 UTC

    Changing the main code to

    package main; use strict; use warnings 'all'; use Devel::Cycle; for( 1...1_000_000 ) { print "\r$_/1000000"; my $top = Top->new(); my $bottom = $top->{bottom} or die "NO BOTTOM!"; $top = $bottom->{top} or die "NO TOP!"; find_cycle( $top ); find_cycle( $bottom ); }

    the memory usage on my box remains steady at 2476 kB, so your memory leak has to be somewhere else. Your code as posted is fine so far.

      You are correct.

      Execute the following on both Perl 5.8.8 and Perl 5.10.0 and I get different results.

      Under Perl 5.8.8, I leak no memory and everything runs fine.
      john@john-laptop:~/Projects/Cloud-T3n$ time perl refs.pl 1000000/1000000 real 0m53.021s user 0m10.753s sys 0m1.912s
      Uses 200Mb of RAM.
      john@john-laptop:~/Projects/Cloud-T3n$ time /usr/bin/perl refs.pl 1000000/1000000 real 0m58.048s user 0m11.989s sys 0m1.672s
      Uses 2,476Kb RAM.

      Please Note that I did as many folks right here have recommended and compiled my Perl 5.10.0 from the source on CPAN, without threads.

      Perhaps we'll see some discussion on that here as well.

      For reference, this is the exact code I am running:
      package Top; sub new { my $class = shift; my $s = bless { }, $class; $s->{bottom} = Bottom->new( $s ); return $s; } #===================================== package Bottom; use Scalar::Util 'weaken'; sub new { my ($class, $top) = @_; my $s = bless { top => $top }, $class; weaken($s->{top}); return $s; } #===================================== package main; use Devel::Cycle; for( 1...1_000_000 ) { print "\r$_/1000000"; my $top = Top->new(); my $bottom = $top->{bottom} or die "NO BOTTOM!"; $top = $bottom->{top} or die "NO TOP!"; find_cycle( $top ); find_cycle( $bottom ); }

        FIXED!

        It turns out that by adding this to class Top the memory leakage (on Perl 5.10.0) went away.:
        sub DESTROY { my $s = shift; undef(%$s) }