in reply to Is there a penalty for namespace use?
But even if I remove it, and not call DESTROY myself, and take all references to the objects out of scope, the memory is not reused. So can anyone show how to prevent the memory doubling in the following? I use 2 different hashes, and undef the first, so why isn't the memory available for the second hash to reuse? I would guess that it is somehow not releasing the array's used in $self->{stuff}, but if I put @{$self->{stuff}} = () in the DESTROY sub, it dosn't clear it.
#!/usr/bin/perl use warnings; use strict; my %bubs; my %bubs1; for(1.. 500){ $bubs{$_} = ZtkBubject->new( -name => $_, ); $bubs{$_}=(); delete $bubs{$_}; $bubs{$_}=(); } %bubs = (); undef %bubs; print "Check memory now and hit enter\n"; <>; for(1.. 500){ $bubs1{$_} = ZtkBubject->new( -name => $_, ); } print "Check memory now and hit enter, its doubled\n"; <>; #===================================================================== +======== # ZtkBubject Class #===================================================================== +======== package ZtkBubject; use strict 'vars'; use Carp; sub new { my ($class, %arg) = @_; my $self = { 'name' => $arg{-name}, }; bless $self; # print "just blessed $self\n"; $self->{stuff} = [1..1000]; #add some memory usage return $self; } ############################################# sub DESTROY{ my ($self) = @_; @{$self->{stuff}} = (); print "destroying->", $self->{name},' ',$self,"\n"; } ########################################### 1;
|
|---|