Garbage Collection Advice

Within the flash.system.System class is a new method called pauseForGCIfCollectionImminent(). The method accepts a single argument that determines the desired imminence value. This optional value must be a Number between 0 and 1 where lower values indicate a less intense need for a garbage collection sweep to occur. Using this method, a developer can advise the garbage collector to begin performing its tasks at a time when it is convenient to do so.

In the example below, we create a small “game” with two states. One of these states represents a play level which the user would interact with and is a timed level. The second state represents the paused time between levels, in which the user is provided with a chance to reflect upon their achievements and prepare for another level of play. With System.pauseForGCIfCollectionImminent() invoked during the proper time, a developer can advise the garbage collector to perform its actions at the best possible time.

package { import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.system.System; import flash.text.TextField; import flash.text.TextFormat; import flash.utils.Timer; [SWF(width="600", height="500", backgroundColor="#CCCCCC")] public class GCAdvice extends Sprite { private var traceField:TextField; private var stateName:String; private var levelTimer:Timer; private var msg:String; public function GCAdvice() { generateDisplayObjects(); performOperations(); } protected ...

Get What's New in Flash Player 11 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.