zentara has asked for the wisdom of the Perl Monks concerning the following question:
With a module however, no matter what I do, the "new" causes quite a memory gain on the second use, even if I try to load the objects into a reusable hash. So, I'm sure this is a common question, but is there a way to completely return a destroyed objects memory, so it can be reused? Or is there just an unavoidable penalty for creating a new namespace?
#this is a pure hash, and it works as I expect #!/usr/bin/perl use warnings; use strict; my %bubs; for(1.. 500){ $bubs{$_}{'stuff'} = [1..1000]; print "adding $_\n"; } <>; for(1.. 500){ $bubs{$_}=(); delete $bubs{$_}; print "deleting $_\n"; } <>; for(1.. 500){ $bubs{$_}{'stuff'} = [1..1000]; print "adding $_\n"; } for(1.. 500){ $bubs{$_}=(); delete $bubs{$_}; print "deleting $_\n"; } <>; __END__ #################################### # this is with objects, where it dosn't # reuse a DESTROYED objects space even though I # stuff them into the same hash space #!/usr/bin/perl use warnings; use strict; my %bubs; for(1.. 500){ $bubs{$_} = ZtkBubject->new( -name => $_, ); $bubs{$_}->DESTROY; delete $bubs{$_}; $bubs{$_}=(); } <>; for(1.. 500){ $bubs{$_} = ZtkBubject->new( -name => $_, ); $bubs{$_}->DESTROY; } <>; #===================================================================== +======== # ZtkBubject Class #===================================================================== +======== package ZtkBubject; use strict 'vars'; use Carp; sub new { my ($class, %arg) = @_; # object attributes my $self = { 'name' => $arg{-name}, #identifying name }; bless $self; # print "just blessed $self\n"; $self->{self} = $self; $self->{stuff} = [1..1000]; #add some memory usage return $self; } ############################################# sub DESTROY{ my ($self) = @_; print "destroying->", $self->{name},' ',$self,"\n"; } ########################################### 1; __END__
2004-12-29 Janitored by Arunbear - added readmore tags, as per Monastery guidelines
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is there a penalty for namespace use?
by dave_the_m (Monsignor) on Dec 29, 2004 at 22:38 UTC | |
by diotalevi (Canon) on Dec 30, 2004 at 04:20 UTC | |
|
Re: Is there a penalty for namespace use?
by osunderdog (Deacon) on Dec 29, 2004 at 23:34 UTC | |
|
Re: Is there a penalty for namespace use?
by revdiablo (Prior) on Dec 29, 2004 at 23:09 UTC | |
|
Re: Is there a penalty for namespace use?
by zentara (Cardinal) on Dec 30, 2004 at 12:47 UTC | |
|
Re: Is there a penalty for namespace use?
by jbrugger (Parson) on Dec 30, 2004 at 06:39 UTC | |
by demerphq (Chancellor) on Dec 30, 2004 at 16:23 UTC | |
|
Re: Is there a penalty for namespace use?
by zentara (Cardinal) on Dec 30, 2004 at 11:48 UTC |