In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

メモリ管理の細々した事。

Get Adobe Flash player
by djakarta_trap 10 Aug 2009
/**
 * Copyright djakarta_trap ( http://wonderfl.net/user/djakarta_trap )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/s7Cp
 */

package {
    import flash.display.Sprite;
    import net.hires.debug.Stats;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.system.*;
    import flash.net.*;
    
    [SWF(backgroundColor="#FFFFFF", frameRate=120)]
    
    public class MemoryTest extends Sprite {
        
        private var _container:Sprite;
        private var _counter:int;
        
        public function MemoryTest() {
            // write as3 code here..
            _container = new Sprite();
            this.addChild(_container);
            this.addChild(new Stats());
            
            this.addEventListener(Event.ENTER_FRAME,_enterFrameHandler);
        }
        
        private function _enterFrameHandler(e:Event):void
        {
            if(_counter > 1000){
                 this.removeEventListener(e.type,_enterFrameHandler);
                 this.addEventListener(MouseEvent.CLICK,_clickHandler);
                 return;
            }
            var color:uint = Math.random()*0xffffff;
            var sp:Sprite = new Sprite();
            sp.graphics.beginFill(color);
            sp.graphics.drawCircle(0,0,20);
            sp.graphics.endFill();
            sp.x = Math.floor(Math.random()*100)/100*stage.stageWidth;
            sp.y = Math.floor(Math.random()*100)/100*stage.stageHeight;
            _container.addChild(sp);
            _counter++;
            
        }
        
        
        private function _clickHandler(e:MouseEvent):void
        {
            this.removeChild(_container);
            /*
            * null にしないと解放されない。
            */
            _container = null;
            
            //強制的にGC発動
            System.gc();
	    try {
  		 new LocalConnection().connect('foo');
		 new LocalConnection().connect('foo');
	        }catch (e:*) {}
        }
            
            
    }
}