in reply to Re^2: Garbage Collection and undef
in thread Garbage Collection and undef

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.

Replies are listed 'Best First'.
Re^4: Garbage Collection and undef
by tilly (Archbishop) on Oct 17, 2004 at 05:08 UTC
    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.

      What's the reason for having ReleaseAction when End already exists on CPAN (and was uploaded before)? If there is no profound reason, why not deprecate one of them and incorporate it in the other?

      ihb

      Read argumentation in its context!

        Because I didn't know about End?

        If you want, blame merlyn. I originally wasn't going to release ReleaseAction. But he thought that it was an interesting module, noted how it made the core module SelectSaver trivial to implement, and encouraged me to release on CPAN. I think that if either of us had realized that it already existed, I wouldn't have released.

        You're right that the two are very similar. Mine is more flexible though. I allow you to cancel the action before it happens. I allow you to pass arguments into the closure. (I don't document that very clearly though.) I give you an optional OO interface.

        While I would not have released had I realized that the same module was out there already, at this point I see no harm in having both available. I'll update my module to mention End though.

Re^4: Garbage Collection and undef
by hv (Prior) on Oct 17, 2004 at 10:50 UTC

    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