Click to See Complete Forum and Search --> : Help with Flash AS3 Garbage Collection memory


Dudecles
07-13-2010, 04:08 PM
Hi, I've created an as3 flash file that reads an .xml with a list of .swf's and calls them sequentially as the user presses the left and right arrow keys to move from one to the next.

However, i've noticed that each time I move from one to another, the amount of memory windows is using keeps increasing, even though I'm replacing the movieClip each time, and removing it from the stage.

It is critical that I find a way to prevent this build-up of memory because it results in a crash after 30-50 switches (up to 1.5GB ram).

here is the code for replacing the MovieClip currently:


function replaceClip(e:Event) {
removeChild(currentSWF);
currentSWF = null;
currentSWF = e.target.content;
addChild(currentSWF);

trace("SWFreplaced");
trace(currentSWF.totalFrames);
}


and the full code:


stop();

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);
stage.addEventListener(MouseEvent.CLICK, clickHandler);

var slideListArray:Array = new Array();
//var clipArray:Array = new Array();
var currentSlideNum:Number = 0;

var currentSWF:MovieClip = new MovieClip();

var slideXML:XML;
var slide_xml_loader:URLLoader = new URLLoader();
slide_xml_loader.load(new URLRequest("slides.xml"));
slide_xml_loader.addEventListener(Event.COMPLETE, slide_xml_loaded);

function slide_xml_loaded(e:Event) {
trace("loaded");
slideXML = new XML(e.target.data);
populateArray();
}

function populateArray() {
for(var i:int; i < slideXML.SLIDE.length(); i++) {
slideListArray.push(slideXML.SLIDE[i].*);
trace("xml= " + slideListArray[i]);
}
loadSlideInit();
}

function loadSlide() {
var slideLoader:Loader = new Loader();
var slideRequest:URLRequest = new URLRequest(slideListArray[currentSlideNum]);
slideLoader.load(slideRequest);
slideLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, replaceClip);
}

function loadSlideInit() {
var slideLoader:Loader = new Loader();
var slideRequest:URLRequest = new URLRequest(slideListArray[currentSlideNum]);
slideLoader.load(slideRequest);
slideLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, initClip);
}

function initClip(e:Event) {
currentSWF = e.target.content;
addChild(currentSWF);

trace("SWFreplaced");
trace(currentSWF.totalFrames);
}

function replaceClip(e:Event) {
removeChild(currentSWF);
currentSWF = null;
currentSWF = e.target.content;
addChild(currentSWF);

trace("SWFreplaced");
trace(currentSWF.totalFrames);
}

function keyPressHandler(e:KeyboardEvent) {
if(e.keyCode == Keyboard.LEFT) {
trace("a key pressed!" + e.keyCode);
currentSlideNum -= 1;
loadSlide();
}
if(e.keyCode == Keyboard.RIGHT) {
trace("a key pressed!" + e.keyCode);
currentSlideNum += 1;
loadSlide();
}
}

function clickHandler(e:MouseEvent) {
trace("clicked!");
if(currentSWF.currentFrame == currentSWF.totalFrames) {
trace("END OF MOVIECLIP");
currentSlideNum += 1;
loadSlide();
}
else {
currentSWF.play();
}
}




Thank you,

infinityspiral
07-29-2010, 03:11 PM
If an object has event listeners that still reference it, it won't be garbage collected so you'll need to remove those as well.