in reply to Does Perl have garbage collection mechanism and how it performs?

Perl's garbage collection is implemented through reference counting (everything has a reference count, when the reference count drops to 0 the 'object' is removed). This can be demonstrated with a simple lexical variable and Devel::Peek
use Devel::Peek; my $foo; Dump($foo); { my $r = \$foo; Dump($foo); } Dump($foo); __output__ SV = NULL(0x0) at 0x8107e34 REFCNT = 1 FLAGS = (PADBUSY,PADMY) SV = NULL(0x0) at 0x8107e34 REFCNT = 2 FLAGS = (PADBUSY,PADMY) SV = NULL(0x0) at 0x8107e34 REFCNT = 1 FLAGS = (PADBUSY,PADMY)
There we can see $foo has an initial REFCNT (reference count) of 1 created by the file-level lexical scope, it is incremented when $r created a reference to it, and then decremented when $r goes out of scope (garbage collected because it's enclosing scope exitted and it's REFCNT dropped to 0) and would be garbage-collected once the file-scoped exits (which in this case is when the program ends). See. Matts wonderful Proxy Objects article for a more thorough review of reference counting in perl.
HTH

_________
broquaint

  • Comment on Re: Does Perl have garbage collection mechanism and how it performs?
  • Download Code