Archive for July 9th, 2008

09
Jul

Memory Managment - Memory Leak

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Now one of the biggest features of as3 is the management of memory within flash and flex.

In my experience event listeners are the source of about 95 percent of all memory leaks. This is because event listeners are often forgotten by developers, which normally results in the listener never being removed from memory.

This is where weakly referenced event listeners come into their own.

Weakly references to objects that are not counted by the Garbage Collector in determining an object’s availability for collection. So if you forget to remove the listener you wont stop the Garbage Collector’s ability to collect the object and freeing your memory. Yay…

So from that stand point alone I suggest that you use Weakly reference Event listeners as standard practice.

To make your event listener weakly then just set the fith param in the addEventListner to true. See code below

stage.addEventListener(Event.CLICK, handleClick, false, 0, true);

Nod to the wondeful gSkinner for this.

Found this useful? How about buying me a coffee - simply click here.




Close
E-mail It