You can monitor your memory usage/leaking with System.totalMemory property. This way you can see how memory is used, where leaks are and you can optimize those parts that are eating too much memory.
In simple words use System.totalMemory as bellow (in KB):
var memoryUsedInKb:Number= Number(System.totalMemory/1024).toFixed(2);
I used it to show the used memory in different units: bytes, Kilobytes, Megabytes, Terrabytes (I hope you’ll never reach TB in your developed applications - that will be a huge memory leak).
private function calculateUsedBytes(unit:String):String { var p:uint = pows[unit.toLowerCase()]; var t:Number = Number(System.totalMemory/Math.pow(2,p)) var d:uint = digits[unit.toLowerCase()]; return t.toFixed(d).toString(); }
The whole code is at the end and now the application…
The code:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="237" creationComplete="init()" height="255"> <mx:TextInput x="67" y="131" id="stringToRepeat" text="abcdefghijklmnopqrstuvw" toolTip="The string that will be added"/> <mx:Label x="9" y="133" text="String:"/> <mx:Label x="9" y="159" text="Repeat:"/> <mx:NumericStepper x="67" y="157" id="timesToRepeat" stepSize="10" minimum="0" maximum="1000000" width="99" toolTip="How many times the string will be added" value="10000"/> <mx:Button x="174" y="157" label="+" width="22" click="handleAlocate(event)" toolTip="Alocate"/> <mx:Label x="9" y="203" text="Memory:"/> <mx:HRule x="9" y="187" width="217" height="8"/> <mx:HRule x="10" y="115" width="217" height="8"/> <mx:Label x="67" y="203" width="159" id="kbUsed" text="{memoryUsed}"/> <mx:ComboBox x="171" y="201" width="55" id="unitCombo" dataProvider="{units}" change="handleUnitChange(event)" toolTip="Units"/> <mx:Label x="9" y="229" text="Length:"/> <mx:Label text="{arrayLength}" x="67" y="229" width="87" toolTip="The length of the array containing the strings"/> <mx:Button x="204" y="157" label="x" width="22" click="handleClear(event)" toolTip="Clear"/> <mx:Text x="10" y="10" text='This is an example to show memory usage. Use the string and the repeat stepper to alocate more memory. Pressing "+" button will add the string n times as selected into repeat to an array. The "x" button is for clearing out the array.' width="217" height="102"/> <mx:Script> <![CDATA[ [Bindable] private var units:Array = [{label:"B",data:"b"},{label:"KB",data:"K"}, {label:"MB",data:"m"},{label:"TB",data:"t"}]; private var pows:Object = {b:0,k:10,m:20,t:30}; private var digits:Object = {b:0,k:0,m:2,t:4}; [Bindable] private var memoryUsed:String; [Bindable] private var arrayLength:uint; private var useDump:Array = new Array(); private function init():void { memoryUsed = calculateUsedBytes("k"); unitCombo.selectedIndex = 1; arrayLength = useDump.length; } private function calculateUsedBytes(unit:String):String { var p:uint = pows[unit.toLowerCase()]; var t:Number = Number(System.totalMemory/Math.pow(2,p)) var d:uint = digits[unit.toLowerCase()]; return t.toFixed(d).toString(); } private function handleUnitChange(event:Event):void { memoryUsed = calculateUsedBytes(unitCombo.selectedItem.data); } private function handleAlocate(event:MouseEvent):void { var r:uint = timesToRepeat.value; var s:String = stringToRepeat.text; for (var i:uint = 0; i < r; i++) useDump.push(s); memoryUsed = calculateUsedBytes(unitCombo.selectedItem.data); arrayLength = useDump.length; } private function handleClear(event:MouseEvent):void { useDump = new Array(); memoryUsed = calculateUsedBytes(unitCombo.selectedItem.data); arrayLength = useDump.length; } ]]> </mx:Script> </mx:Application>
Popularity: 43%
Tags: bytes, leaks, memory, monitoring, usage
This post was written by Andrei Ionescu
Views: 1909


















