PDA

View Full Version : Will a self reference prevent garbage collection?



ls_ac
December 24th, 2009, 09:35 PM
Will a self reference prevent garbage collection?

eg:


public class Foo
{
private var _self:Foo;

public function Foo() { _self = this; }
}

...

var f = new Foo();
f = null;


Will the instantiation of Foo be garbage collected after f is set to null, or will it's self-reference keep it in memory?

Krilnon
December 24th, 2009, 09:53 PM
No, your Foo will still be eligible for collection because there won't be a chain of references leading from one of the root memory objects to your Foo instance. It'd survive the reference counting phase, but not the mark and sweep phase, I suppose.

ls_ac
December 25th, 2009, 02:06 AM
Hm. That makes sense. Thanks.