Freeing up memory for Garbage Collection

Something that I’ve been noticing lately with Flash / Actionscript 3 is the need to pay close attention to how you are cleaning up your objects after you are done with them.

I’ve been working with a great as3 debugging tool called “De MonsterDebugger”. Strange name but great tool. Check it out here: http://www.demonsterdebugger.com/

Basically you just download the debugger and import the class:

1
import nl.demonsters.debugger.MonsterDebugger;

then as the first line in your constructor function:

1
new MonsterDebugger(this);

Now you just open the air application that came with the download and it will track everything that happens in your class in the interface. You can run functions from it as well. It rocks!

Anyway, my point being that as I was running it on some of my programs I noticed a significant amount of memory leakage on some of the classes. Here are a few tips to help flash along with garbage collecting your old used up objects.

One thing I like to do is to create a destroy() function at the end of my classfiles. The job of this function is to reset all variables possible in the script. I’ll explain more in a moment.

The ‘delete’ keyword:

This is a somewhat misunderstood keyword in as3 because it only works on dynamically created variables. let me explain. take the following code for example:

1
2
3
4
5
6
7
8
9
10
11
var fluffy:Cat = new Cat("happy");
var sally:Cat = new Cat("angry");

// now somewhere along the way we want to remove fluffy
delete fluffy;
// oops! This generates an error. This is because the delete
// keyword only works on dynamic properties.
fluffy = null;
// Ok, so this sets fluffy to null but references to fluffy elsewhere
// in the code may still cause Garbage Collection to not be able to
// clean it from memory.

So I have discovered that the only way to be sure that your flash variables will be garbage collected and the memory will be released is to make the variables in your classes dynamic by using arrays or objects to store them.

You could do this like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var cat:Object = new Object();

cat["fluffy"] = new Cat("angry");
cat["sally"] = new Cat("happy");

addChild(cat.sally);
addChild(cat.fluffy);

// ok, now later we can clean up with a destroy function

function destroy():void{

        for(var i:Object in cat){
                removeChild(cat[i]);
                delete cat[i];
        }
}

The destroy function iterates through all objects inside of the cat object and removes them from the display list.
Next, it deletes the property itself freeing it up for garbage collection.

Keep in mind that the Cat class should also have a destroy function that cleans itself out as well!
So the function would be modified to look like this:

1
2
3
4
5
6
7
8
9
function destroy():void{

        for(var i:Object in cat){

                removeChild(cat[i]);
                cat[i].destroy();
                delete cat[i];
        }
}

You can use the De MonsterDebugger to magically watch your memory performance increase dramatically by using this technique. Happy Flash coding!

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

2 thoughts on “Freeing up memory for Garbage Collection”

Leave a Reply