Wednesday, November 10, 2010

Displaying system memory usage using AS3

In this tutorial we will find out how to display system memory usage in flash.

When you are developing a complex application or a game that will potentially cause lag on the machine, it is important to monitor the current system memory usage for debugging reasons. To do this, you can use the property System.totalMemory, which displays memory being used by Flash Player in bytes.

The code below will create a text field in the top left corner of your flash that will update every frame and show you how much memory is your player currently eating.

import flash.text.TextField;

var memoryText:TextField = new TextField();
memoryText.height = 20;
memoryText.width = 50;
addChild(memoryText);
memoryText.selectable = false;

stage.addEventListener(Event.ENTER_FRAME, memoryDisplay);

function memoryDisplay(Event):void
{
memoryText.text = Number(System.totalMemory / 1024 / 1024).toFixed(2) + "Mb";
}

Hope this was helpful. Thanks for reading!

1 comment:

Jimmy C said...

Cool beans! I hope to get this code implemented for QA testing right away. Thanks!

Post a Comment