I’m working with loading external bitmaps. 4 every 10 seconds in fact. It is essential that I unload my images from memory, or else eventually my SWF is going to slow down dramatically due to the amount of memory it is using.
Unfortunately, I can’t seem to get my Bitmaps to unload. You would think “unload()” would do it. Unload() DOES remove the loader data out of the loader, but it doesn’t remove it from memory. I have a trace(System.totalMemory) running and I can see my memory usage go up and up.
I have heard of this being called a “memory leak”, and and issue with Flash 9 Player. I have also heard that the problem isn’t a memory leak, but the inability of the Garbage Collector to pick up unloaded bitmaps.
I’ve stepped away from my project and I have just set up a simple routine with two buttons on the stage. One which loads an image, and one which supposedly removes the image from the memory completely, or that is the aim. Can anyone help me with the code below, as I want to to remove the image completely from memory each time. Maybe I am missing a reference to the loaded bitmap which is making the GC not pick up the unloaded bitmap?
Now, I am also aware that the GC doesn’t dispose things at our beck and call. It cleans up when it sees fit. But I have run the below code for 10+ minutes, clicking the load and unload buttons over and over to see if my memory eventually goes down, which it does not. RSI anyone?
Also you will see the Localconnect hack which supposedly helps this issue, but it doesn’t seem to do anything for me…
package{
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.utils.Timer;
import fl.transitions.easing.*;
import flash.text.*;
import flash.net.*;
import flash.events.*;
import fl.transitions.TweenEvent;
import fl.transitions.Tween;
import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.system.*
public class Foobar extends MovieClip{
public var urlLoader:URLRequest;
public var loader:Loader = new Loader();
public function Foobar(){
loadIt.addEventListener(MouseEvent.MOUSE_UP, loadTheImage, false, 0, true);
unloadIt.addEventListener(MouseEvent.MOUSE_UP, unloadTheImage, false, 0, true);
}
public function loadTheImage(ev:MouseEvent):void{
urlLoader = new URLRequest("brent.jpg");
loader.load(urlLoader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
}
public function unloadTheImage(ev:MouseEvent):void{
trace("bai, im going to unload now");
loader.unload();
try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
} catch (e:*) {}
// the GC will perform a full mark/sweep on the second call.
trace(System.totalMemory);
}
public function loadComplete(ev:Event):void{
trace("hai, im loaded");
loader.removeEventListener(Event.COMPLETE, loadComplete);
//addChild(loader.content);
}
}
}