Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Anyone around who's familiar with garbage collection? I've got a mem leak, and I'm reasonably sure I found it (I put a print in DESTROY), but the mem. doesn't get released ... The doc's on this subj. are not really detailed, so I started wondering iff I may count on the mem. being released in short order. Can I?

Replies are listed 'Best First'.
Re: garbage collection guts
by mirod (Canon) on Nov 13, 2000 at 18:06 UTC

    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.

Re: garbage collection guts
by merlyn (Sage) on Nov 13, 2000 at 17:50 UTC
    Have you read the FAQ entry on this? Memory used and free-d by Perl is not released to the O/S (except in MacOS, if I recall correctly), so the apparent memory usage will not go down, but that memory is still available for Perl to re-use for other items.

    -- Randal L. Schwartz, Perl hacker

      Memory can be released to the OS on any system that supports it. If Perl is compiled to use the system malloc() (rather than its own internal version of malloc()) then it does whatever the system malloc() does. All of which would be an academic point, except that on GNU/Linux systems with glibc2, the system malloc() does return memory to the O/S if it thinks that might be beneficial.

      I was really surprised when I found this out. My Perl program was chugging along, and it suddenly got smaller. I couldn't believe my eyes. I had to spend the next hour digging into the source code of the free() function until I found the responsible code.

      The guy who wrote that malloc() package is a genius.

      Which FAQ is that? I read the OO faqs (perltoot,perlobj, perlbot, as well as searched PAN && perlmonks 4 garbage collection), but appart from the stuff about self-reffing obj. in the BOT, I did not find anything other than the OOstuff. Your answer explains what is happening here, anyway, so thanks. But I would like a pointer so that I can read up for myself.