Perl manages memory automatically: variables are destroyed when there are no more references to them (reference counting). The garbage collector in Perl does not use typical tracing GC, but relies on reference counting.
Advantages:
free(), as in C).Disadvantages and pitfalls:
my $a = {}; my $b = {}; $a->{b} = $b; $b->{a} = $a; # Both variables are not freed on cleanup, perl cannot delete them
To solve such problems, the Scalar::Util::weaken module is used, which allows you to "weaken" a reference:
use Scalar::Util 'weaken'; my $a = {}; my $b = {}; $a->{b} = $b; weaken($b->{a} = $a);
Are any Perl objects eliminated when all explicit variables are deleted, even if there are references between them?
Answer: No! If objects reference each other (create a cycle), Perl will not delete them — it will require manually breaking the cycle or weakening the reference via Scalar::Util::weaken.
Story
While developing a long-term daemon working with a large number of connections, programmers did not notice a circular reference between the IoHandle object and the associated event handler. After several hours of operation, the memory grew exponentially — only analysis with Devel::Leak revealed the issue.
Story
In the ETL process of parsing large files, the accumulation of millions of temporary hash elements led to the "hanging" of the process even after the cycle completed. This happened because one of the elements held a nested reference to its parent (for three-level references) and memory was not being freed. Partial restructuring of the scheme helped avoid the leak.
Story
Programmers used closures in the MapReduce engine, storing copies of context in anonymous subroutines. These subroutines "leaked" — memory was not freed even after the batch task completed, as the context contained references to themselves. An explicit
undefwas added for proper destruction.