in reply to garbage collection guts

As mentioned recently in the Acky Memory thread Perl's Garbage Collector is based on reference count. Any variable (actually any piece of memory ever allocated so I guess this also includes lists for example) as an associated reference count. This count is incremented when the variable is created (to 1!) and every time another variable references it. It is decremented when the variable goes out of scope or when a reference does not reference it anymore. Once the count gets to 0 the memory is freed (and available to the rest of the process but not to the OS as merlyn noted).

The problem with this (usually quick and efficient) scheme is with circular references: if 2 variables reference each other then even when they get out of scope their reference count stays at 1 and the memory never get released. This is quite frequent with complex structures, such as trees for example, where a parent might reference a child which in turn references the parent.

Fixing this problem involves either "breaking the loop" in the DESTROY method (while not destroying too much though), or using the Devel::WeakRef module which allows you to create references that do not increase the reference count (warningI have never used that module and I don't know it's state, especially I have no idea if the patch to the Perl core that comes with it is still valid, if it has been integrated in the core or if the module cannot be used with 5.6, I'd be glad to learn more about it though!).

Item 34 of Effective Perl Programming has a nicely illustrated example of circular data structure and how to deal with it.