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

Garbage Collection Hack

「ガベージコレクションを働かせる裏技」
http://f-site.org/articles/2011/07/31220748.html
Get Adobe Flash player
by Fumio 31 Jul 2011
    Embed
/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/11Q8
 */

package {
    import flash.display.Sprite;
    import flash.utils.getTimer;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.net.LocalConnection;
    import flash.system.System;
    import flash.utils.Timer;
    [SWF(width = "240",height = "180")]
    public class GarbageCollectionHack extends Sprite {
        private var my_txt:TextField = new TextField();
        private var my_fmt:TextFormat = new TextFormat();
        private var nCount:uint = 0;
        private var myTimer:Timer = new Timer(500, 1);
        public function GarbageCollectionHack() {
            createTextField();
            var mySprite:Sprite = new Sprite();
            stage.addEventListener(MouseEvent.CLICK, xStop);
            mySprite.addEventListener(Event.ENTER_FRAME, test);  // , false, 0, true);
            mySprite = null;
            myTimer.addEventListener(TimerEvent.TIMER, xGetMemory);
        }
        private function test(eventObject:Event):void {
            xTrace(nCount++);
        }
        private function xStop(eventObject:MouseEvent):void {
            gcHack();
            myTimer.start();
        }                    
        private function gcHack():void {
            // System.gc();
            try {
                new LocalConnection().connect('test');
                new LocalConnection().connect('test');
            } catch (errorObject:Error) {}
        }
        private function createTextField():void {
            addChild(my_txt);
            my_fmt.align = TextFormatAlign.LEFT;
            my_txt.defaultTextFormat = my_fmt;
            my_txt.autoSize = TextFieldAutoSize.LEFT;
        }
        private function xTrace(n:int):void {
            var _str:String = "loop: " + String(n);
            _str += "\nmemory: " + String(System.totalMemory);
            _str += "\nclick on the stage to garbage-collect!"
            my_txt.text  = _str;
        }
        private function xGetMemory(eventObject:TimerEvent):void {
            var _str:String = "\nmemory is reduced to: ";
            _str += String(System.totalMemory);
            my_txt.appendText(_str);
            myTimer.removeEventListener(TimerEvent.TIMER, xGetMemory);
        }
    }
}