Garbage colleciton of Flex : with Example

November 19, 2007

Flash Player provides built-in garbage collection that frees up memory by destroying
objects that are no longer used. To ensure that the garbage collector destroys your
unused objects, remove all references to that object, including the parent’s reference
to the child.

On containers, you can call the removeChild() or removeChildAt() method to remove
references to child controls that are no longer needed. The following example removes
references to button instances from the myVBox control:

example:

<![CDATA[
private function destroyButtons(e:Event):void {
myVBox.removeChild(b1);
myVBox.removeChild(b2);
myVBox.removeChild(b3);
}
]]>

You can clear references to unused variables by setting them to null in your ActionScript; for example:

myDataProvider = null

To ensure that destroyed objects are garbage collected, you must also remove event
listeners on them by using the removeEventListener() met