Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Timing of garbage collection

by blue_cowdawg (Monsignor)
on Jan 18, 2013 at 18:15 UTC ( #1014102=note: print w/ replies, xml ) Need Help??


in reply to Timing of garbage collection

      So, just how undesirable is it to manually call object destructors?

I heard Larry Wall talk about GC in Perl about eleven years ago and I'll paraphrase him here: "Something lives in memory in Perl until nobody cares."

From what I remember there is a reference count associated with any variable (or object) and when that reference counter drops to zero the object is freed from memory.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re: Timing of garbage collection
Re^2: Timing of garbage collection
by tobyink (Abbot) on Jan 18, 2013 at 18:39 UTC

    Indeed...

    use strict; use warnings; use Test::More; use Scalar::Util qw(weaken isweak); use Devel::Refcount qw(refcount); my $A = bless {}, 'Monkey'; is(refcount($A), 1, 'an object has a refcount'); my $B = $A; is(refcount($A), 2, 'additional ref to the object increases the refcou +nt'); $B = undef; is(refcount($A), 1, 'removing a ref to the object decreases the refcou +nt'); do { my $C = $A; is(refcount($A), 2, 'reference in a scope increases refcount'); }; is(refcount($A), 1, '$C fell out of scope, so decreased ref count'); my $D = $A; is(refcount($A), 2, 'another ref which will increase ref count'); weaken $D; is(refcount($A), 1, 'weakening the ref means it is not included in the + ref count'); ok($A == $D, 'yet $A and $D are still the same object'); my $destroyed = 0; sub Monkey::DESTROY { $destroyed++ }; $A = undef; is($destroyed, 1, 'undefining $A destroyed the object; $D was a weak r +ef so did not prevent destruction'); ok(!defined $D, '$D is now undefined'); done_testing;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
    /tt); my $B = $A; is(refcount($A), 2,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2015-07-04 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls