...or more specifically, I have a class TestSequence, which has two relevant methods to my question:
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; }
Now suppose we create some low-level data and later prune higher up the tree:
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' );
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)?

(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


In reply to How clever is the garbage collector? by Moron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.