Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Clearing an Array of Array's from memory

by Anonymous Monk
on Aug 29, 2003 at 15:36 UTC ( #287715=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of arrays (or at least that is what I think it is) that I would like to clear from memory when the program is done using it (the program then continues on without it). Here is an example of how the array of arrays is created in case it is not what I think it is:

push(@{$this_array{$call_one}},$data);

My question is two part:
1.) How do I clear this array of arrays from memory?
2.) This is a farily large array, so will the act of clearing it from memory take time (i.e. will it slow my program down by clearing it)?

Thanks much in advance.

Comment on Clearing an Array of Array's from memory
Download Code
Re: Clearing an Array of Array's from memory
by bear0053 (Hermit) on Aug 29, 2003 at 15:43 UTC
    You can undef the array by doing the following:
    undef(@myarray);
    This will make @myarray = undef. I don't believe clearing it out will slow down your program any despite the size of the array.
      This will make @myarray = undef.

      I suspect this is just a case of casual, imprecise language, but you are likely to mislead people with that particular phrasing:

      my @myarray = undef; print "\@myarray contains ", 0+@myarray, " elements.\n"; __END__ Produces: @myarray contains 1 elements.
      Because     @myarray = undef;
      is the same as     @myarray = ( undef );
      which isn't at all the same as     undef @myarray;
      undef is a function that undefines what is passed to it and then returns a not-defined() scalar value.

                      - tye
Re: Clearing an Array of Array's from memory
by tcf22 (Priest) on Aug 29, 2003 at 16:03 UTC
    You could just assign it an empty array
    @myarray = ();
    or if it is an array ref, then assign it to an empty array ref
    $myarray_ref = [];
    Cleaning it up will take a little time, but not enough to worry about. Doing this will free up some memory(it won't release memory back to the OS) for perl to reuse.
Re: Clearing an Array of Array's from memory
by blue_cowdawg (Monsignor) on Aug 29, 2003 at 16:08 UTC

        How do I clear this array of arrays from memory?

    I believe it was Tom Christiansen that said during a talk on Perl at a Lisa Conference "a variable in Perl only lives as long as someone cares about it". Meaning that if you undef it or otherwise stop referencing it Perl will get rid of it. What is not clear to me is if Perl will release the memory back to the system or if it will just make it available for other variables to use.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
    Chat Stuff: AIM:  redcowdawg
    Yahoo: blue_cowdawg
    Cowdawg Philosophy:  Sieze the Cow! Bite the Day!
    Clever Quip:  Nobody expects the Perl Inquisition!
    Non-Perl Passion:   Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

      Freed variables are made available for other variables. The system only gets it back when the process ends.

      Update: Fixed typo.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

        This is incorrect. Whether the freed space is returned to the operating system is OS, OS version, C library, and allocation size dependent. Memory is released back to the OS in many places, including Linux and Win32 (which are two of the more common OSes perl's used on)
Re: Clearing an Array of Array's from memory
by hardburn (Abbot) on Aug 29, 2003 at 16:54 UTC

    Most of the time when I see someone clearing memory that way, they're not taking advantage of use strict and lexical scoping.

    Instead of manually "freeing" the array, declare it my within the block of code you need it in. Perl will automatically get rid of it when you don't need it anymore. See also, Lexical scoping like a fox.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Letting a variable fall out of scope doesn't free up all its memory. The memory used for the members of an aggregate (array or hash) are generally freed, however the memory used for the structure of the variable (including scalars) aren't freed--they are instead saved in case the scope is re-entered. Notice:
      use Devel::Size qw(size); $foo = 2; while ($foo--) { { my $bar; print Devel::Size::size(\$bar), " ", length($bar), "\n"; $bar = " " x 10000000; } }
      prints
      12 0 10000025 0
      not
      12 0 12 0
      which is what you'd expect if the memory was actually freed on scope exit.
Re: Clearing an Array of Array's from memory
by Elian (Parson) on Aug 29, 2003 at 17:17 UTC
    undef'ing the array will throw it up and delete any otherwise unreferenced things in the array, or the arrays in the array. This won't take a noticeable amount of time except in the most extraordinary of circumstances. (Like everything in the array of arrays has been swapped out to disk and needs to be paged back in to alter reference counts)

    The memory may or may not be released back to the system--that depends mainly on the size of any one array, but generally not. On many systems you need to get into the megabyte allocation range (which means one element that's multi-megabytes in size, or an array of millions of elements) for this to happen, but some operating systems and C memory allocation libraries are more aggressive than others in returning memory to the system.

    Making this a my variable and letting it fall out of scope will release all the memory except for the memory taken by the structure (but not the contents) of the my'd variable. So a multi-million element array falling out of scope will release its contents, but the structure of the array itself will remain. (This is one of the space/speed tradeoffs that perl makes)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-10-31 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (217 votes), past polls