I've been toying around with modules, and with Tk I know you need to reuse a module's object's namespace, or you get a memory gain. So I decided to try and simplify it to it's simplest form, to see what is going on. So in the examples below, if I use a straight hash to store data, I can load, empty, and reuse the hash with no memory gain on the second load.

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__


I'm not really a human, but I play one on earth. flash japh

2004-12-29 Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to Is there a penalty for namespace use? by zentara

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.