[转载]AS3强制内存回收的方法

注:AVM的GC机制确实有些诡异,不象CLR可以通过GC.Collect()显示调用,中午在天地会闲逛时,发现了下面的这二个方法,转载一下

原文:http://bbs.9ria.com/viewthread.php?tid=24027&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D12&page=1

方法1:

function GC():void{
                try{
                (new LocalConnection).connect("foo");
                (new LocalConnection).connect("foo");}catch(e){
                        trace(System.totalMemory);
                        }
                }

方法2:

package {
        /**
         * @author CYPL
         * @date 2009-04-01
         */
        import flash.net.SharedObject;
        import flash.system.System;
        import flash.utils.clearInterval;
        import flash.utils.setInterval;        

        public class GCPlus {
                public static function clear(isTraceTM : Boolean = false) : void {
                        var time : int = 2;
                        var interval : int = setInterval(loop, 50);
                        function loop() : void {
                                if(!(time--)) {
                                        isTraceTM && trace(System.totalMemory);
                                        clearInterval(interval);
                                        return;
                                }
                                SharedObject.getLocal("cypl", "/");
                        }
                }
        }
}

测试代码:

package {
        import flash.display.MovieClip;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.system.System;        

        /**
         * @author CYPL
         */
        public class GC_Test extends Sprite {
                private static const NUM : int = 1000;
                private var num : int;

                public function GC_Test() {
                        init();
                }

                private function init() : void {
                        num = NUM;
                        stage.addEventListener(MouseEvent.CLICK, clickHandler);
                        while (num--) {
                                var mc : MovieClip = new MovieClip;
                                mc.graphics.beginFill(0);
                                mc.graphics.drawRect(0, 0, 100, 100);
                                mc.x = Math.random() * 500;
                                mc.y = Math.random() * 400;
                                addChild(mc);
                        }
                        
                        trace(System.totalMemory);
                }

                private function clickHandler(e : MouseEvent) : void {
                        clear();
                }

                private function clear() : void {
                        while (numChildren) {
                                removeChildAt(0);
                        }
                        GCPlus.clear(true);
                }
                /*function GC():void{//这是我们熟悉的方法
                try{
                (new LocalConnection).connect("foo");
                (new LocalConnection).connect("foo");}catch(e){
                        trace(System.totalMemory);
                        }
                }*/
        }
}

通过System.totalMemory的结果可以清楚的看到内存得以释放,window任务管理器也能够看到有变化哦,原理也可能和异常有点关系.

posted @ 2010-12-09 13:50  菩提树下的杨过  阅读(3459)  评论(0编辑  收藏  举报