Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How clever is the garbage collector?

by Moron (Curate)
on Jun 07, 2006 at 12:33 UTC ( [id://554018]=perlquestion: print w/replies, xml ) Need Help??

Moron has asked for the wisdom of the Perl Monks concerning the following question:

...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

Replies are listed 'Best First'.
Re: How clever is the garbage collector?
by jasonk (Parson) on Jun 07, 2006 at 13:15 UTC

    The formatting of your code is kind of tough to get through, but if I understand correctly you are essentially creating a data structure that looks like this:

    $sq = bless({ BB => [ { 'some hash' => 'number 1' }, { 'some hash' => 'number 2' }, { 'some hash' => 'number ...' }, { 'some hash' => 'number 1_000_000' }, ], }, 'TestSequence');

    And the question is whether running delete( $sq->{BB} ) will cause the garbage collector to trash all of the hashes. The answer is yes, assuming the program doesn't contain any other references to those hashes (including the hashes referencing each other), then the garbage collector will get them all.

    However, you should keep in mind that having the garbage collector destroy those objects will not necessarily cause your program to release the memory they consumed, but that is a different question which has been answered before (several times).


    We're not surrounded, we're in a target-rich environment!
Re: How clever is the garbage collector?
by mickeyn (Priest) on Jun 07, 2006 at 13:15 UTC
    Hi,

    As far as I know and previously searched and found in Perl literature, Perl's GC does NOT release allocated memory back to the OS, but rather reuse it within its own process.

    Enjoy,
    Mickey

Re: How clever is the garbage collector?
by Moron (Curate) on Jun 07, 2006 at 13:25 UTC
    Fortunately, I only needed the memory to be released so that a new set of a million hashes can be generated for the next test in the 'TestSequence', whose single instance will exist throughout a sequence of tests on different data to be iteratively loaded into the hash of array and replaced by different data for the next test run. So both these answers tell me what I wanted to hear -- thanks!

    -M

    Free your mind

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://554018]
Approved by bpphillips
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-16 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found