13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

for each (var item:Object in groupMap)<br />

{<br />

trace(item);<br />

}<br />

/* output:<br />

[object Object]<br />

[object Object]<br />

[object Object]<br />

*/<br />

Object keys and memory managem<strong>en</strong>t<br />

Adobe® Flash® Player and Adobe® AIR use a garbage collection system to recover memory that is no longer used.<br />

Wh<strong>en</strong> an object has no refer<strong>en</strong>ces pointing to it, the object becomes eligible for garbage collection, and the memory is<br />

recovered the next time the garbage collection system executes. For example, the following code creates a new object<br />

and assigns a refer<strong>en</strong>ce to the object to the variable myObject:<br />

var myObject:Object = new Object();<br />

As long as any refer<strong>en</strong>ce to the object exists, the garbage collection system will not recover the memory that the object<br />

occupies. If the value of myObject is changed such that it points to a differ<strong>en</strong>t object or is set to the value null, the<br />

memory occupied by the original object becomes eligible for garbage collection, but only if there are no other<br />

refer<strong>en</strong>ces to the original object.<br />

If you use myObject as a key in a Dictionary object, you are creating another refer<strong>en</strong>ce to the original object. For<br />

example, the following code creates two refer<strong>en</strong>ces to an object—the myObject variable, and the key in the myMap<br />

object:<br />

import flash.utils.Dictionary;<br />

var myObject:Object = new Object();<br />

var myMap:Dictionary = new Dictionary();<br />

myMap[myObject] = "foo";<br />

To make the object refer<strong>en</strong>ced by myObject eligible for garbage collection, you must remove all refer<strong>en</strong>ces to it. In this<br />

case, you must change the value of myObject and delete the myObject key from myMap, as shown in the following code:<br />

myObject = null;<br />

delete myMap[myObject];<br />

Alternatively, you can use the useWeakRefer<strong>en</strong>ce parameter of the Dictionary constructor to make all of the<br />

dictionary keys weak refer<strong>en</strong>ces. The garbage collection system ignores weak refer<strong>en</strong>ces, which means that an object<br />

that has only weak refer<strong>en</strong>ces is eligible for garbage collection. For example, in the following code, you do not need to<br />

delete the myObject key from myMap in order to make the object eligible for garbage collection:<br />

import flash.utils.Dictionary;<br />

var myObject:Object = new Object();<br />

var myMap:Dictionary = new Dictionary(true);<br />

myMap[myObject] = "foo";<br />

myObject = null; // Make object eligible for garbage collection.<br />

Last updated 6/6/2012<br />

40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!