AS3 How-Tos and Tips > Easy Tests for Memory Use and Frame Rate

We present in this How-To simple code snippets that you can use to test the memory consumption and the current frame rate of your Flash movie. We give the code that can be placed on the MainTimeline if you are using a Timeline script. If you are using a Document Class, we give the version of the code to be placed within your Document Class. The code is for testing purposes only. If you are satisfied with your movie's memory and frame rate performance, delete the code before posting your movie.

This How-To applies mostly the case when your movie has one frame and everything is happening as a result of your AS3 script. As we discuss below, that in no way diminishes the importance of frame rate. Frame rate is simply the frequency with which the screen is updated and isn't necessarily related to moving the playhead.

The Timeline Code for Testing Memory

To test the memory consumption of your movie, place the following code at the end of your MainTimeline script.

this.addEventListener(Event.ENTER_FRAME,performMemTest);

 

function performMemTest(e:Event):void {

trace(System.totalMemory);

}

As you test your movie, the total memory used by your movie will be displayed in the Output window of the Flash authoring tool, in bytes. What you do not want to see is a significant, continuous increase in memory as time goes on. Generally, you should see a cyclical behavior: the memory consumption may temporarily increase, but then it should decrease as the garbage collector clears unused assets. For some applications, the memory usage will remain nearly constant. The behavior as well as the number indicating the total memory used depend, of course, on the type of assets in your movie and on how you use them.

Memory Test in the Case of a Document Class

If you use the code from the previous section in the context of a Document Class, the main difference is in having to import the System class: flash.system.System;. Also, you need 'private' or 'public' attributes for your functions and variables. The code that you place in, say, YourDocumentClass.as, with YourDocumentClass assigned as the Document Class to, say, yourFla.fla fla file may look as follows.

..........................

import flash.system.System;

..........................

Within the constructor of YourDocumentClass, you may place:

..........................

this.addEventListener(Event.ENTER_FRAME,performMemTest);

..........................

Outside the constructor:

private function performMemTest(e:Event):void {

trace(System.totalMemory);

}

 

Then test movie in yourFla.fla file. The memory usage will be displayed in the Output widow.

You can, for example, try the code with the files from our tutorial 3D Menu on a Cube with Perspective, Distorting Images in ActionScript 3. Download the package 'menu3d.zip' and enter the above code in FlowerMenu.as which is assigned as the Document Class to menu3d.fla. Then test movie in menu3d.fla.

Testing Frame Rate

The frame rate of your movie, in fps - frames per second - is the frequency with which the screen will be updated as your movie plays. In general, the updates may reflect the playhead moving to the next frame of your movie's Timeline but equally often the playhead is not moving and the updates reflect changes due to the user's interaction, or changes mandated programmatically by your AS3 script. It is the latter case that we address in this How-To. For example, if you animate your objects programmatically, your fla file may have only one frame on the Timeline with AS3 code attached to it or contained in a Document Class. The animation will be created due to refreshing of the screen that happens at the frame rate of your movie and your AS3 script. A better name for the frame rate would be 'screen update rate' or 'scheduled display refresh rate'.

To set the frame rate of your movie in a fla file, click on an empty area of the Stage. In the Properties panel, you see the frame rate field. In Flash CS3, the default frame rate is 12 fps; in Flash CS4 it is 24 fps. You can enter your own values in the field. For a more extensive discussion of frame rate and changing it programmatically see another of our How-Tos: Testing Flash Frame Rate, Huge Differences Between Browsers.

The actual current frame rate of your movie may vary as the movie plays and may differ from the rate set in the Flash authoring environment or programmatically. The Flash Player may not be able to keep up with the scheduled updates of the screen at the set frame rate due to a heavy rendering load or lots of calculations. Thus, the actual frame rate may differ from the one set by you. It also depends on the user's system and very heavily on the browser. To test the frame rate behavior of your swf  in the Test Player, place the following code at the end of your MainTimeline script.

var frames:int=0;

var prevTimer:Number=0;

var curTimer:Number=0;

 

this.addEventListener(Event.ENTER_FRAME,performFrameTest);

 

function performFrameTest(e:Event):void {

frames+=1;

curTimer=getTimer();

if(curTimer-prevTimer>=1000){

trace("FPS: "); trace(Math.round(frames*1000/(curTimer-prevTimer)));

prevTimer=curTimer;

frames=0;

}

}

The function 'getTimer' returns the time, in milliseconds, since a movie opened. The ENTER_FRAME event is fired when the screen is refreshed, so at the current frame rate. The value of the variable 'frames' increases by 1 whenever a refresh happens. At approximately every second, i.e. 1000 millliseconds, we measure how many 'frames' happened in the time elapsed. The approximate current frame rate will be displayed in the Output panel as you test your movie.

You can try the code with, for example, the files from our tutorial 3D Texture Mapping with 'drawTriangles' in Flash CS4 and AS3. Download the package 'cs4planets.zip' and enter the above code in cs4planets.fla. You may want to set the frame rate of the 'cs4planets.fla' file to 32 fps. You may see (depending on your system) that some planets cause the frame rate to drop, namely Venus. Neptun returns it to 32 fps.

If you want to use the code within a Document Class, you need to import flash.utils.getTimer;, declare and initialize three private variables frames, prevTimer, curTimer, add the ENTER_FRAME listener within the constructor and a private method performFrameTest outside of the constructor.

However, if you use 'trace' you are testing the frame rate of your file in the Test Player. That rate can differ hugely from the rate your movie achieves while running within a browser.

See our How-To Testing Flash Frame Rate, Huge Differences Between Browsers for a custom AS3 class that tests both memory and the frame rate and an example that illustrates the differences bewteen browsers and Players.

Back to AS3 How To and Tips              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.