Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Well, a good way to make your memory clean is to use strict, and declare every variable with my. Try to organize your code in subs too, since the variables used in each sub will be cleaned automatically by Perl. In orther words, try to not make global variables.

For who want something better, like clean every thing inside a package, including the memory used to declare subs, try to use this code, that I have adapted to make it independent for this node. The code comes from the module HPL::Safe. Where it runs a script inside a Safe compartment and clean all the memory after each run. Note that a package cleaned with this can't be reused, you will get some memory leaks!

#!/usr/bin/perl clean_pack('main::FOO'); ############## # CLEAN_PACK # ############## sub clean_pack { my ( $pack_name ) = @_ ; &undef_pack($pack_name,1) ; ## Clean fisrt variables to DETROY objec +ts. my @packs = (scan_packs($pack_name) , $pack_name) ; foreach my $packname ( reverse sort @packs ) { &undef_pack($packname +,\%NO_CLEAN) ;} return( 1 ) ; } ############## # UNDEF_PACK # ############## sub undef_pack { my ( $packname , $keep_base) = @_ ; $packname .= '::' unless $packname =~ /::$/ ; no strict "refs" ; my $package = *{$packname}{HASH} ; return unless defined $package ; foreach my $symb ( keys %$package ) { if ( $symb !~ /::$/ && $symb !~ /^(?:\@|_|-|\d|\]|\^[VO]?)$/ ) { undef *{$packname . $symb} ; } } undef *{$packname} if !$keep_base ; } ############## # SCAN_PACKS # ############## sub scan_packs { my ( $package ) = @_ ; my %packs = %{_symdump($package)} ; my @result ; my $prepend ; foreach my $pack (keys %packs){ push @result, map {"$prepend$_"} keys %{$packs{$pack}{$part} || {} +}; } return(@result) ; } ############ # _SYMDUMP # ############ sub _symdump { my(@packages) = @_ ; my($key,$val,$num,$pack,@todo,$tmp); my $result = {}; foreach $pack (@packages){ no strict; while (($key,$val) = each(%{*{"$pack\::"}})) { my $gotone = 0; local(*ENTRY) = $val; #### PACKAGE #### if (defined $val && defined *ENTRY{HASH} && $key =~ /::$/ && $key ne "main::" && $key ne "<none>::") { my($p) = $pack ne "main" ? "$pack\::" : ""; ($p .= $key) =~ s/::$//; $result->{$pack}{PACKAGES}{$p}++; $gotone++; push @todo, $p; } } } return (@todo) ? { %$result, %{_symdump(@todo)} } : $result ; } ####### # END # #######

Graciliano M. P.
"The creativity is the expression of the liberty".


In reply to Re: Memory usage and perl. (Cleaning a Package) by gmpassos
in thread Memory usage and perl by Anonymous Monk

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-10-22 06:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (114 votes), past polls