Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Garbage Collection and undef

by pg (Canon)
on Oct 17, 2004 at 04:12 UTC ( [id://399871]=note: print w/replies, xml ) Need Help??


in reply to Garbage Collection and undef

"does undef just force garbage collection, or does it actually un-declare the my definition"

If it undeclares the array, then the following would have syntax error, but it actually not, so the answer is clear:

use strict; use warnings; my @a = (1,2,3); undef @a; $a[1] = 2; @a = undef; $a[1] = 2;

Also garbage collector does not free memory right after you tell it to, it kicks in according to its own schedule.

Replies are listed 'Best First'.
Re^2: Garbage Collection and undef
by tilly (Archbishop) on Oct 17, 2004 at 04:19 UTC
    Also garbage collector does not free memory right after you tell it to, it kicks in according to its own schedule.

    But Perl 5 uses reference counting, not true garbage collection, so it does free memory immediately.

      It is a misunderstaning that reference counting always causes immediate garbage collection. There are different algorithms, and counting reference is much more complex than you thought. As a matter fact, in order to resolve problems including cyclic garbage, Perl uses Deutsch and Bobrow's deferred reference counting. This algorithm ignores updates to local variables, and it periodically scans the stack to determine the true reference counts.

        Can you find documentation of this claim? The Perl documentation that I've read says that Perl uses classic reference counting and does not handle circular garbage (until it has to free everything when it exits). Which is why we have Devel::WeakRef.

        For instance see Reference Counts and Mortality in perlguts or Two-Phased Garbage Collection in perlobj. You can verify what that says by experimenting with DESTROY methods. They exhibit reliable behaviour that they wouldn't if true GC were in use. Else my ReleaseAction wouldn't work as advertised.

        I'm not sure where you got this information from, but it isn't true. Perl5 uses simple immediate reference counting, and if you remove your last external reference to a cyclic structure without breaking the cycle you'll leak that memory.

        Some additional optimisations are done: notably things pushed temporarily onto the main stack (eg function arguments) usually do not have their reference count increased to reflect the fact; this turns out to be the source of numerous bugs however, and as we've added more exceptions and workarounds to fix those the benefit has decreased to the point we're considering removing the optimisation altogether.

        Perl6 has been designed from the start to use GC, but I don't know much detail about the precise flavour of GC used.

        Hugo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-24 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found