in reply to Can we determine when the garbage collector works?
Perl has no Garbage Collection, because Perl does Reference Counting.
Most likely, you don't code with pointers but with references, and the only thing you have to fear with Reference Counting are circular references, as the reference count for these can never reach zero and thus the memory never be reclaimed.
There are two ways around this - one way is to have an explicit destroy() method that the user is supposed to call - a stupid idea in my opinion, but sometimes less hassle than the second option. Scalar::Util has weaken(), which allows you to create weak references, that is, references which do not increase the reference count for the referenced object. With that, structures containing circular references can also be reclaimed as soon as the last strong reference falls out of scope.
Note that a closure never goes out of scope as the subroutine never gets released, and thus the memory held by the closure cannot be reclaimed automatically (if at all before process exit).
Steve_p points to Devel::Cycle and Test::Memory::Cycle to hunt down the circular references in your programs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Can we determine when the garbage collector works?
by Doraemon (Beadle) on May 12, 2004 at 15:24 UTC | |
by Corion (Patriarch) on May 12, 2004 at 15:30 UTC |