O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Garbage Collection Demonstration

Garbage Collection Demonstration shows a very simple program that demonstrates garbage collection at work. The program creates a Sprite object that displays a message repeatedly in the debugging output console. Because the Sprite object is reachable only via a local variable, it becomes eligible for garbage collection immediately after the program's main class constructor completes. Meanwhile, the program also runs a timer that repeatedly creates objects, occupying system memory. When enough system memory is consumed, the garbage collector runs. During garbage collection, the original Sprite object is removed from memory, and its messages stop appearing in the debugging output console.

Example 14-1. Garbage collection demonstration

package { import flash.display.*; import flash.text.*; import flash.utils.*; import flash.events.*; import flash.system.*; public class GarbageCollectionDemo extends Sprite { public function GarbageCollectionDemo () { // This Sprite object is garbage collected after enough memory // is consumed var s:Sprite = new Sprite(); s.addEventListener(Event.ENTER_FRAME, enterFrameListener); // Repeatedly create new objects, occupying system memory var timer:Timer = new Timer(1, 0); timer.addEventListener(TimerEvent.TIMER, timerListener); timer.start(); } private function timerListener (e:TimerEvent):void { // Create an object to take up some system memory. Could be // any object, but TextField objects are nice and meaty. new TextField(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required