ForbetterCoder
ForbetterCoder

Reputation: 89

WHY does Context become a GCRoot?

I appreciate Robotlegs very much, but recently a GC problem came to me. I failed to dispose context object by just set the reference null.With the help of FB profile tool, I find that context object appears to be a "GC Root". To figure it out, I wirte a simple class, which creates a context obj and leave it unreachable.Here is the detail of this class:

public class MemoryLeak extends Sprite{
    public function MemoryLeak()
    {
        makeAndDrop();
    }

    public function makeAndDrop():void{
        var _context = new Context(this);
        _context = null;
    }
}

When I ran this class, I hoped it be disposed by GC, but it didn't work(most times, not everytime). And the profile tool show me this instance is a GCRoot. I read some articles about GC, but few of them mention GCRoot itself. Could anybody tell me why and thank you so much!

PS: I tried to call System.gc() twice after makeAndDrop() but it didn't work. In fact, I'm more interested in the "is GCRoot" issue(implied by the fb profile), it may help more if you tell me about it.

Upvotes: 3

Views: 231

Answers (1)

Amy Blankenship
Amy Blankenship

Reputation: 6961

I think the Context will probably listen to this so that it can perform dependency injection on any added children or create mediators for them. One would hope that the listener would not be attached until you talk to the mediatorMap or the viewMap, but I think it is likely that the RL authors would not consider a use case where you'd want a Context on a View for a time period shorter than the View's actual lifespan.

Upvotes: 0

Related Questions