Actionscript内存管理,垃圾回收

Fra*_*rth 2 flash actionscript garbage-collection memory-management actionscript-3

此博客(和其他人)声明在清理对象时应该在dispose()方法中将对象引用设置为null.

但是,Actionscript 3(使用Flash Player 9)使用标记和扫描来清除循环引用.所以我想知道:是否真的有理由废弃您的对象引用?

Mar*_*rty 6

我从来没有 - 只要你做了明显的事情:

  • 中断对象的所有引用(从数组中删除,将存储对象的变量设置为null,从显示列表中删除)
  • 删除所有事件侦听器等

然后,对象使用的内存可随时覆盖.

var ar:Array = [];
var mc:MovieClip = new MovieClip();

mc.addEventListener(MouseEvent.CLICK, pants);

ar[ar.length] = mc;
addChild(mc);

if(mc.parent) mc.parent.removeChild(mc); // not garbage collected
mc.removeEventListener(MouseEvent.CLICK, pants); // still not garbage collected
ar.splice(0, 1); // finally garbage collected
Run Code Online (Sandbox Code Playgroud)