Discover the social platform for Creative Talents, or Register

My Profile

eyelogic
Job

Flash Developer

Competence/s

Flash, Flex, Audio

View full profile

My Latest Galleries

My Connections

kuat
  • kuat
  • 2 posts/2 galleries
  • Offline

Blog Item

Back to Blog

Memory management in Flash Player 9 - a little helper class

Since the release of ActionScript 3.0 and Flash Player 9, memory management (garbage collection) is a very serious topic for Flash developers.

Here is a simple helper class i use in my projects to be always aware of the memory usage of the Flash Player.
Just create a file MemoryControl.as, save it in the root folder of your project and add the following code:

package de.eyelogic {

import flash.display.Sprite;
import flash.events.Event;
import flash.system.System
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
import flash.events.TimerEvent;

/**
* ...
* @author Wanja Stier
*/
public class MemoryControl extends Sprite{
private var tf:TextField;
private var mb:Boolean;
private var timer:Timer;

public function MemoryControl(mb:Boolean, updateFreq:int, color:uint)
{
//set mb to true or false
this.mb = mb;

tf = new TextField();
tf.textColor = color;
tf.autoSize = TextFieldAutoSize.LEFT;
addChild(tf);

//set up the timer object and pass the update frequency in milliseconds to it
//invoke updateMemoryUsage on each TimerEvent.Timer
timer = new Timer(updateFreq);
timer.addEventListener(TimerEvent.TIMER, updateMemoryUsage);
timer.start();
}
private function updateMemoryUsage(e:TimerEvent):void
{
//if mb is true, display memory usage in MB, else display it in KB
if (mb)
{
tf.text = (Math.round (((System.totalMemory / 1024)/1024) * 10)) / 10 + " MB";
}
else
{
tf.text = (Math.round ((System.totalMemory / 1024) * 10)) / 10 + " KB";
}

}
}

}

usage:

//parameter 1: if first parameter is set to true, memory usage will be displayed in MB, if set to false it will be displayed in KB
//parameter 2: sets the update frequency in milliseconds
//parameter 3: sets text color of the text field instance
memoryControl = new MemoryControl(true, 500, 0xFFFFFF);
memoryControl.y = 400;
memoryControl.x = 50;
addChild(memoryControl);


Categories:

interactive design, technology/development

Tags:

Flash Memory

You must login or register to post a comment.


Send this page to a friend!