package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.net.LocalConnection; import net.hires.debug.Stats; // このクラスを実行すると、最終的に 8.3 MB 程度のメモリを消費します。 // ※消費量は実行環境によって差異があります。 public class MemoryTest extends Sprite { public function MemoryTest() { stage.addChild( new Stats() ); // 大量の MyObject インスタンスを作成する var tests:Array = []; while ( tests.length < 100000 ) { tests.push( new MyObject() ); } // stage クリックで参照を破棄する stage.addEventListener( MouseEvent.CLICK, function( e:MouseEvent ):void { tests = []; try { // 強制的に GC を発動する new LocalConnection().connect( "foo" ); new LocalConnection().connect( "foo" ); } catch ( e:Error ) {} } ); } } } class MyObject { public function MyObject() { // クラスメソッドをローカル変数に代入して破棄する var value:Function = myMethod; value = null; } public function myMethod():void { } } メモリにゴミが残る(問題版)