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. | [reply] [d/l] [select] |
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) | [reply] [d/l] [select] |
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. | [reply] [d/l] |
|
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 | [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
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. |
| [reply] |
|
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
| [reply] |
|
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)
| [reply] |