I read this really good article on how Flash does garbage collection: http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html

But I was wondering what all you would have to do to make sure all your objects were garbage collected by the faster reference counting method rather than the more CPU intensive mark and sweep method. Suppose I have the following function and create an object with it:

function makeIt():void {
    var spriteA:Sprite = new Sprite();
    var spriteB:Sprite = new Sprite();
    spriteB.addEventListener(MouseEvent.CLICK, myCallback);
    var spriteC:Sprite = new Sprite();
    spriteA.addChild(spriteB);
    spriteB.addChild(spriteC);
    stage.addChild(spriteA);
}

If I just did the following, would it only be eligible for GC by mark and sweep?

spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
stage.removeChild(SpriteA);

Parents have references to their children, and children have references to their parents, so would I have to do all this to make it eligible for GC by reference count?

spriteB.removeChild(spriteC);
spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
spriteA.removeChild(spriteB);
stage.removeChild(spriteA);
spriteA = null;

Would I have to set spriteB and spriteC to null as well? And what if spriteC was instead an instance of a class that extended Sprite and had several of its own properties. Would I have to null those too?

有帮助吗?

解决方案

Yes, you'll have to removeChild, removeEventListener and set local references (or class level references) to get reference count to zero.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top