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

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.

Replies are listed 'Best First'.
Re^4: Memory leaks and circular references
by jdrago_999 (Hermit) on Sep 07, 2008 at 00:56 UTC
    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) }