Perl's uses reference counting as its method of doing garbage collection. As soon as the last reference to a value is released, the value is destroyed and freed. This has both advantages and disadvantages over Java's approach.
A disadvantage over Java's method is that you have to worry about circular references which will cause a leak.
push @{ $parent->{children} }, $child; weaken( $child->{parent} = $parent ); # Needed to avoid memory leak.
An advantage over Java's method is that you don't have to worry about when the object is destroyed, as it will be destroyed the moment is ceases being referenced. This avoids subtle, intermittent, timing-dependent errors.
{ Resource res = get_resource_exclusively(res_id); // ... res.release(); // Needed or the next line might fail. } { Resource res = get_resource_exclusively(res_id); // ... res.release(); }
You might even need a try/catch block to ensure timely destruction in some places.
Resource res = get_resource_exclusively(res_id); try { // ... } catch (Exception e) { res.release(); throw e; }
In reply to Re: Perl and Garbage Collection
by ikegami
in thread Perl and Garbage Collection
by Svetlana
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |