Archive for July, 2008

11
Jul

Finding Memory Leaks - Memory Usage Guage

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

Following on from my previous post about memory leaks I thought I would show you the code I use for spotting for memory leaks at runtime.

See below

import flash.events.*;
import flash.system.System;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;

var tf:TextField = new TextField();
tf.border = true;
tf.textColor = 0x333333
tf.background = true
tf.autoSize = TextFieldAutoSize.LEFT;
addChild(tf) 

initTimer();

function initTimer():void {
	var myTimer:Timer = new Timer(1000, 0);
	myTimer.addEventListener("timer", timerHandler1);
	myTimer.start();
}

function timerHandler1(event:TimerEvent):void {
	tf.text = String(Math.round(flash.system.System.totalMemory /1000 /1000)) + " MB \n " + String(flash.system.System.totalMemory) + " Bytes";
}

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

09
Jul

Memory Managment - Memory Leak

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