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?