Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Clearing an Array of Array's from memory

by hardburn (Abbot)
on Aug 29, 2003 at 16:54 UTC ( #287751=note: print w/ replies, xml ) Need Help??

in reply to Clearing an Array of Array's from memory

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

Comment on Re: Clearing an Array of Array's from memory
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Clearing an Array of Array's from memory
by Elian (Parson) on Aug 29, 2003 at 17:26 UTC
    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; } }
    12 0 10000025 0
    12 0 12 0
    which is what you'd expect if the memory was actually freed on scope exit.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://287751]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2015-11-29 10:08 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls