开发者

garbage collection best practices

If you're removing a MovieClip from the display list, and that MovieClip in turn has child MovieClips which have their own event listeners, is it necessary to remove ALL listener开发者_开发技巧s from the child MovieClips?

or just the parent MovieClip that is being directly removed from the display list?


It depends if the listeners attached, to either the parent or children MovieClips, have weak references pointing to it or not.

When you add a listener, you can use the last parameter to set if the listener will use a weak reference. This is exactly what you need to know for the question you ask.

//This listener will use a weak reference, notice the last "true"
something.addEventListener("event", myFunction, false, 0, true);
//This is called a weak reference listener.
//The ussual listener, with default parameters, is a strong refence listener.

EventDispatcher Reference

So an object will be garbage collected if all the references to such object are deleted. Listeners added with the default parameters count toward those references (since the last parameter default value is false). So having a MovieClip with a strong reference listeners attached to it or any of its children, by removing it from the display list the clip will NOT be garbage collected until the listener references are also deleted (by using the removeEventListener method).

If you use weak references in a clip or any of its children, by removing it from the display list it will eventually be garbage collected. Have in mind that this may occur after some time, so events like ENTER_FRAME may be still be triggered and called until the object is finally collected.


I think is enough to removeChild(MC), then MC=null or delele MC, I guess delete will do the work id you read the specifications from Adobe, I think you can also call System.gc but this is for AIR apps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜