Hi all,
I’ve got a pretty big issue here that I can’t understand. I’ve got a pretty simple app that just repeatedly loads and then unloads an .swf (this loaded .swf contains nothing but a document class and a graphic - a circle. The document class is blank, it just extends Sprite).
When I set this class as the document class of a blank .swf, I trace the ‘totalMemory’ of the flash player, and load and unload the (very simple) clip over and over. You’d think that with garbage collection and all that, the memory would stay pretty constant…but no! It increases (and continues to for as long as I’ve run it!). This seems very bad. Any ideas??
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.events.Event;
import flash.net.LocalConnection;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.system.System;
public class memoryLeak extends Sprite {
private var loader:Loader;
private var memory:Number;
public function memoryLeak() {
var timer:Timer = new Timer(300);
timer.addEventListener(TimerEvent.TIMER, traceMemory);
timer.start();
}
private function traceMemory(event:TimerEvent):void {
if (memory != System.totalMemory) trace(System.totalMemory); // only trace changes in totalMemory
memory = System.totalMemory;
loadSWF();
}
private function loadSWF():void {
if (loader != null) removeSWF();
loader = new Loader();
var urlRequest:URLRequest = new URLRequest("test.swf");
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
loader.load(urlRequest, context);
addChild(loader);
}
private function removeSWF():void {
removeChild(loader);
loader.unload();
loader = null;
// force immediate garbage collection - see grant skinner's article at
// http://www.gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html
try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
} catch (e:*) {}
}
}
}