AS3.0 Memory monitoring
I just came across the property System.totalMemory. It's very useful, especially for the engine I am currently working on, and dealing with AS3 garbage collection.
It returns the memory currently being used by the Flash Player. It's in bytes so it pays to clean it up slightly.
Actionscript:
-
var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';
-
trace( mem ); // eg traces "24.94Mb"
It seems this takes account of all instances of the flash player. For example, I have a logger/debug panel running in the sidebar of firefox, built in flex. As soon as I open this and it's initialised, it uses an extra 5 - 10mb of ram. So be warned, if you see a massive increase in memory usage, check you don't have loads of sites open with ad banners or other flash based stuff.
26 Comments so far
Leave a reply
[...] AS3.0 Memory monitoring » This Summary is from an article posted at pixelbreaker on Friday, November 09, 2007 I just [...]
Really useful! Thanks man!
thanks you.......
Is this the same debugger you mentioned as your original post of the "kernel debugger"? You never did release this and I for one would love to see it in action/code.
no this has nothing to do with the debugger.
[...] Pixel Breaker has a good post that shows you how to convert bytes to Mega bytes. pixelbreaker : AS3.0 Memory monitoring [...]
thanks for all
This will come in handy when cleaning my bike.
But it is wrong.
You calculated MiB. If you want to know how
much MB you should divide twice throug 1000 and
NOT 1024 :)
This is one of the shortest most useful snippets of code I've ever came around.
As somebody mentioned there is a small inaccuracy in your code. You are indeed calculating megabyte which is expressed in short as MB, not Mb (megabits). I think that is what Timon is referring to.
Keep up the good work and thanks for sharing!
this the same debugger you mentioned as your original post of the "kernel debugger"? You never did release this and I for one would love to see it in action/code.
This is great stuff. Now when I have access to the tool, I would love to ask some Memory Monitoring 101 questions. In the example it registers at 25MB, I get 28MB. Is that span (25-30MB) considered a reasonable value for an empty flash file?
Another simple question is what would be a good limit to try and stay within?
Thanks for sharing. Really appreciate it.
aşk şiirleri
aşk şiirleri
aşk şiirleri
Thnaks You!......
Thank you from Poland
Very useful. My first class is a memory monitoring thingamajig :) This helps make it more visual.
Speaking about monitoring... You can post my ResourceMonitor class, which is very useful while developing apps:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2008 Julius Lomako / julious.loa@gmail.com
// You are free to use this class in any commercial project,
// to distribute, change etc.
// @version 1.0
// @author Julius Lomako a.k.a. JLoa
//
////////////////////////////////////////////////////////////////////////////////
package org.jloa.display
{
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.Sprite;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.system.System;
import flash.utils.setInterval;
import flash.utils.clearInterval;
/**
* ResourceMonitor class provides the fps and used memory information
* represented in a textfield instance;
*/
public class ResourceMonitor extends Sprite
{
/** Current memory amount used by FlashPlayer [mb] */
public var memory:Number = 0;
/** Current fps value */
public var fps:int = 0;
/** The current average fps value according to the history records (if showFpsAVG=true)*/
public var fpsAVG:int = 0;
/** Shows the textfield with resource info [@default=true] */
public var showText:Boolean = true;
/** Shows the graph monitor [@default=false] */
public var showGraph:Boolean = false;
/** Shows the graph grid [@see graphStyle.gridColor] [@default=false] */
public var showGrid:Boolean = false;
/** Shows the average fps according to the history records [@default=false] */
public var showFpsAVG:Boolean = false;
/**
* Defines the style to apply to the graph
* Properties: {width:int,height:int,gridColor:uint,bgColor:uint, lineColor:uint,lineSize:int}
* [mind: works only if showGraph=true]
*/
public var graphStyle:Object = {width:80, height:40, gridColor:0xFFFFFF, bgColor:0x000000, lineColor:0xFFCC00, lineSize:2};
private var info:String = "";
private var tf:TextField;
private var to:TextFormat = new TextFormat("Verdana","9",0x000000);
private var frames:uint = 0;
private var init:Boolean = true;
private var graph:Sprite = new Sprite();
private var graphFps:Sprite = new Sprite();
private var fpsMax:uint = 0;
private var historyLength:int = 0;
private var history:Array;
private var avg:Number;
private var n:int;
private var interval:uint;
/**
* Constructor
* @param showText:Boolean shows the text info (if set true) or not (false) [@default=true]
* @param showGraph:Boolean shows the graphic monitor (if set true) or not (false) [@default=false]
* @param historyLength:int defines the max number of stored history records [@default=15]
*/
public function ResourceMonitor(showText:Boolean = true, showGraph:Boolean = false, historyLength:int = 15)
{
super();
this.showText = showText;
this.showGraph = showGraph;
this.historyLength = historyLength;
}
//////////////////////////////////////////
//
// PUBLIC
//
//////////////////////////////////////////
/**
* Inits the monitor
*/
public function monitor():void
{
if(stage)
{
if(getChildByName("label")) removeChild(tf);
if(getChildByName("graph")) removeChild(graph);
clearInterval(interval);
removeEventListener(Event.ENTER_FRAME, countFrames);
initHistory();
if(showText)
{
tf = new TextField();
tf.name = "label";
tf.defaultTextFormat = to;
tf.width = graphStyle.width;
tf.multiline = true;
tf.selectable = false;
tf.autoSize = TextFieldAutoSize.LEFT;
addChild(tf);
}
if(showGraph)
{
graph.name = "graph";
graph.graphics.beginFill(graphStyle.bgColor,1);
graph.graphics.moveTo(0,0);
graph.graphics.lineTo(graphStyle.width, 0);
graph.graphics.lineTo(graphStyle.width, graphStyle.height);
graph.graphics.lineTo(0, graphStyle.height);
graph.graphics.lineTo(0, 0);
graph.graphics.endFill();
if(showGrid)
{
drawGrid();
}
addChild(graph);
graph.addChild(graphFps);
graph.visible = false;
}
interval = setInterval(monitorResources,1000);
addEventListener(Event.ENTER_FRAME, countFrames);
}else{
throw new Error("**ResourceMonitor ** class instance not found in the DisplayObject list. Use addChild() method before using the public monitor() method.",0);
}
}
/**
* Returns the string representation of the class name, the current fps,mem values;
* @return String - string representation '[[ResourceMonitor memory: memory fps: fps fpsAVG: fpsAVG]'
*/
override public function toString():String
{
return "[ResourceMonitor memory: "+memory+" fps: "+fps+" fpsAVG: "+fpsAVG+"]";
}
/**
* TextField reference
*/
public function get label():TextField
{
return tf;
}
//////////////////////////////////////////
//
// PRIVATE
//
//////////////////////////////////////////
/** @private counts the fps, memory */
private function monitorResources():void
{
fpsMax = stage.frameRate;
fps = (frames>fpsMax)?fpsMax:frames;
fpsAVG = countFpsAVG();
frames = 0;
memory = Math.floor(System.totalMemory/(1024*1024)*100)/100;
if(fpsMax/fpsFPS: "+fps+"/"+fpsMax;
}else{
info = "MEM: "+memory+"mbFPS: "+fps+"/"+fpsMax;
}
if(showFpsAVG){
info+="FPS AVG: "+fpsAVG;
}
if(showText)
{
tf.htmlText = info;
}
if(showGraph)
{
history.splice(0,1);
history.push({fps:fps,memory:memory});
drawGraph();
}
if (init) {
dispatchEvent(new Event(Event.INIT));
init = false;
graph.visible = true;
}else{
dispatchEvent(new Event(Event.CHANGE));
}
}
/** @private frame counter */
private function countFrames(event:Event):void
{
frames++;
}
/** @private draws the graph */
private function drawGraph():void
{
graph.y = (showText)?int(tf.height+1):0;
graphFps.graphics.clear();
graphFps.graphics.lineStyle(graphStyle.lineSize, graphStyle.lineColor, 1, true, "normal", CapsStyle.SQUARE, JointStyle.MITER, 3);
for (var i:int=0; i 0)?history[i].fps:fpsMax)/fpsMax)*graphStyle.height);
}else{
graphFps.graphics.lineTo(Math.round((graphStyle.width/history.length)*i),(((history[i].fps>0)?history[i].fps:fpsMax)/fpsMax)*graphStyle.height);
}
}
graphFps.graphics.endFill();
}
/** @private draws the graph grid */
private function drawGrid():void
{
for(var i:int=0;i
thanks
film izle film seyret
U can find the ResourceMonitor class @ www.chargedweb.com/labs/
Hope u find it useful. Cheers ^_^
Thanks so much for this. Works like a charm.
One small suggestion: You may want to get a clean version out of subversion for your download sample. I had conflicts when incorporating it into my project.
many many thanks.
I've recently created another class, which would be useful 4 u guys. It's called 'Cache' and it cleans memory dumped of garbage ^_^
Check it out @ my labs http://chargedweb.com/labs/
I have been looking for this, thank you! :)
Thanks for the tip and the cache class, it's proving very useful to me.
I have a general question: what is generally acceptable memory usage for a flash / AIR app? One I'm working on runs at between 7-10 MB using the Cache class, but using the Activity monitor on My Mac, it shows the real memory usage as in the region of 28MB. Is this ok?
thanks for all
Your site is very easy in terms of expression and open. I think everyone who enters your site is very gratifying, but also sharing a very nice opportunity to give ...
süper site..Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.
thanks for all man :)