Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Understanding garbage collection specifics...

by cLive ;-) (Prior)
on Feb 01, 2007 at 09:24 UTC ( [id://597708]=note: print w/replies, xml ) Need Help??


in reply to Re: Understanding garbage collection specifics...
in thread Understanding garbage collection specifics...

I knew that, but I was wondering why undef does free the memory and why Perl doesn't just do that automatically.

Replies are listed 'Best First'.
Re^3: Understanding garbage collection specifics...
by dave_the_m (Monsignor) on Feb 01, 2007 at 10:41 UTC
    As an optimisation, perl often doesn't completely clear lexical arrays on scope exit. It frees the elements of the array, but the block of memory it has to hold pointers to the 1E6 elements isn't freed. This is on the assumption that if you entered the block once and created a big array, you're likely to do so again. On the other hand, undef frees the pointer block too.

    Dave.

      So, basically, best practice would be to leave the undefs in the "startup subs" that load and parse XML configuration when the daemon is starting up, but remove them from the trigger subs that get called regularly throughout its life?
        So, basically, best practice would be to leave the undefs in the "startup subs" that load and parse XML configuration when the daemon is starting up, but remove them from the trigger subs that get called regularly throughout its life?
        Well 95% of the time, best practice would just be to not worry about it. If memory's really tight, you probably shouldn't be writing your daemon in Perl in the first place :-)

        For those occasions where a particular scope is entered only once, and a lexical is set to be a long string, then undef before scope exit will free the space for that string, and undeffing a large lexical array will free 4*@array bytes on a 32-bit system.

        Dave.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-28 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found