jdrago_999 has asked for the wisdom of the Perl Monks concerning the following question:
package Top; sub new { my $class = shift; my $s = bless { }, $class; $s->{bottom} = Bottom->new( $s ); return $s; } #===================================== package Bottom; sub new { my ($class, $top) = @_; return bless { top => $top }, $class; } #===================================== package main; use strict; use warnings 'all'; use Devel::Cycle; my $top = Top->new(); find_cycle( $top );
Cycle (1): $Top::A->{'bottom'} => \%Bottom::B $Bottom::B->{'top'} => \%Top::A
I burn through 200Mb of RAM in 42 seconds.local $| = 1; for( 1...1_000_000 ) { print "\r$_/1000000"; my $top = Top->new(); }
package Top; use Scalar::Util 'weaken'; sub new { my $class = shift; my $s = bless { }, $class; weaken($s); $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); return $s; } #===================================== package main; use strict; use warnings 'all'; my $top = Top->new(); my $bottom = $top->{bottom} or die "NO BOTTOM!";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Memory leaks and circular references
by chromatic (Archbishop) on Sep 06, 2008 at 20:37 UTC | |
by jdrago_999 (Hermit) on Sep 06, 2008 at 22:14 UTC | |
Re: Memory leaks and circular references
by moritz (Cardinal) on Sep 06, 2008 at 20:38 UTC | |
by jdrago_999 (Hermit) on Sep 06, 2008 at 22:16 UTC | |
by shmem (Chancellor) on Sep 07, 2008 at 00:02 UTC | |
by jdrago_999 (Hermit) on Sep 07, 2008 at 00:56 UTC | |
by jdrago_999 (Hermit) on Sep 07, 2008 at 01:05 UTC | |
| |
Re: Memory leaks and circular references
by Animator (Hermit) on Sep 07, 2008 at 18:56 UTC |