PDA

View Full Version : Memory Leak when loading .swfs



slashripshred
June 13th, 2007, 02:57 PM
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:*) {}
}


}
}

dthought
June 15th, 2007, 03:38 AM
The force immediate GC "trick" was stopped in the latest Flash player from memory - but perhaps RezMason has some better ideas here. Are you de-referencing everything properly?

slashripshred
June 15th, 2007, 12:22 PM
Hi,

Yes, I am dereferencing properly...turns out it's another issue altogether. Although this memory leak is apparent in the IDE, changing the code a bit to output the totalMemory to a textfield and testing in the browser achieves different results - with the memory being garbage collected almost immediately and never going above a fixed point. Seems like a (bug?) Flash IDE / Debug Player issue.

mikemanh
January 16th, 2008, 01:46 PM
Hi,

Yes, I am dereferencing properly...turns out it's another issue altogether. Although this memory leak is apparent in the IDE, changing the code a bit to output the totalMemory to a textfield and testing in the browser achieves different results - with the memory being garbage collected almost immediately and never going above a fixed point. Seems like a (bug?) Flash IDE / Debug Player issue.


Was the output window growing and growing?
I notice significant decreases in performance and available memory the longer trace statements accumulate in the output window. Try running the same thing with "omit trace actions" turned on, in the IDE.