Moron has asked for the wisdom of the Perl Monks concerning the following question:
Now suppose we create some low-level data and later prune higher up the tree:package TestSequence; sub define { # set value anywhere in motley hash and # return it for conformity with '=' # parameters: reference, key(s)..., value # the recursion simply walks down the list # of keys my $self = shift; my $key = shift my $value = shift; if ( @_ ) { # need to go lower down the tree if (( ref( $self ) eq 'HASH' ) ||(ref($self) eq 'TestSequence')) { defined $self -> { $key } or $self -> { $key }{ $value } = undef(); return define( $self -> { $key }, $value, @_ ); } if ( ref( $self ) eq 'ARRAY' ) { defined $self -> [ $key ] or $self ->[ $key ] = undef(); return define( $self -> [ $key ], $value, @_ ); } die 'not a classmember nor a hash nor array reference'; } if ( ref( $self ) eq 'HASH' ) { return $self -> {$key} = $value; } return $self -> [$key] = $value; } sub prune { # prune the tree where specified by the key list my $self = shift; my $key = shift; if ( @_ ) { if (( ref( $self ) eq 'HASH' ) ||(ref( $self ) eq 'TestSequence')) { return prune( $self -> { $key }, @_ ); } else { return prune( $self -> [$key], @_ ); } } if (( ref( $self ) eq 'HASH' ) ||( ref( $self ) eq 'TestSequence' )) { return delete $self -> { $key ); } return delete $self -> [$key]; } sub new { # just to clarify... my $self = shift; $self = {}; return bless $self; }
Does anyone know if the delete (in method prune) will release the memory used by the one million hashes in this example? Or does it just release the memory used by the single node that is explicitly deleted, $sq -> { BB } in this case? Would the same be true if the one million hashes were replaced by references to one million objects from some other class (assuming the array referenced by $sq -> { BB } contains the only remaining references to those objects)?my $sq = TestSequence -> new(); $sq -> define( 'BB', undef() ); for (my $i = 0; $i < 1000000; $i++ ) { my %hash; # fill the hash, then later... $sq -> { BB }[ $i ] = \%hash; } # do something useful with $sq, then later... $sq -> prune( 'BB' );
(Updated to take into account that ref(object) returns class name - an omission in the example code written from memory that isn't in fact in the real code)
Thanks in advance,
-M
Free your mind
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How clever is the garbage collector?
by jasonk (Parson) on Jun 07, 2006 at 13:15 UTC | |
Re: How clever is the garbage collector?
by mickeyn (Priest) on Jun 07, 2006 at 13:15 UTC | |
by Limbic~Region (Chancellor) on Jun 07, 2006 at 15:02 UTC | |
Re: How clever is the garbage collector?
by Moron (Curate) on Jun 07, 2006 at 13:25 UTC |