Skip to content

Instantly share code, notes, and snippets.

@fljot
Created December 15, 2011 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fljot/5a277685cfcbed6e8ccb to your computer and use it in GitHub Desktop.
Save fljot/5a277685cfcbed6e8ccb to your computer and use it in GitHub Desktop.
Vector unloading
Dependencies:
http://minimalcomps.googlecode.com/files/MinimalComps_0_9_10.swc
http://minimalcomps.googlecode.com/files/MinimalComps_0_9_10.zip
http://mrdoob.googlecode.com/svn/trunk/libs/net/hires/debug/Stats.as
package
{
import flash.display.Sprite;
public class Module extends Sprite
{
// uncomment this to see memory leak
//private var vComplex:Vector.<Sprite> = new Vector.<Sprite>();
// this will always unload properly
private var vSimple:Vector.<Number> = new Vector.<Number>();
}
}
package
{
import com.bit101.components.PushButton;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class Shell extends Sprite
{
private var loader:Loader;
public function Shell()
{
new PushButton(stage, 10, 10, "load", load);
new PushButton(stage, 10, 50, "unload", unload);
}
private function load(event:Event):void
{
if (loader)
return;
loader = new Loader();
addChild(loader);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain();
loader.load(new URLRequest("Module.swf"), context);
}
private function unload(event:Event):void
{
if (!loader)
return;
removeChild(loader);
loader.unloadAndStop(true);
loader = null;
}
}
}
or massive:
package
{
import net.hires.debug.Stats;
import com.bit101.components.PushButton;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.LocalConnection;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class MassiveModuleLoader extends Sprite
{
private static const N:uint = 50;
private var counter:int;
public function MassiveModuleLoader()
{
addChild(new Stats());
new PushButton(this, 100, 10, "run " + N + " times", run);
new PushButton(this, 100, 40, "force GC", gc);
}
private function run(event:Event):void
{
counter = N;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function gc(event:Event):void
{
try {
new LocalConnection().connect("foo");
new LocalConnection().connect("foo");
}
catch (e:*){}
}
private function onEnterFrame(event:Event):void
{
if (counter-- <= 0)
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
return;
}
var loader:Loader = new Loader();
addChild(loader);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
loader.load(new URLRequest("Module.swf"), context);
}
private function onLoaderComplete(event:Event):void
{
var loader:Loader = (event.target as LoaderInfo).loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaderComplete);
removeChild(loader);
loader.unloadAndStop(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment