Chapter 14. Garbage Collection
Every time a program creates an object, ActionScript stores it in system memory (for example, in RAM). As a program creates hundreds, thousands, or even millions of objects, it slowly occupies more and more memory. To prevent system memory from being fully depleted, ActionScript automatically removes objects from memory when they are no longer needed by the program. The automatic removal of objects from memory is known as garbage collection.
Eligibility for Garbage Collection
In an ActionScript program, an object becomes eligible for garbage collection as soon as it becomes unreachable. An object is unreachable when it cannot be accessed directly or indirectly through at least one garbage collection root. The most significant garbage collection roots in ActionScript are as follows:
Package-level variables
Local variables of a currently executing method or function
Static variables
Instance variables of the program’s main class instance
Instance variables of an object on the Flash runtime display list
Variables in the scope chain of a currently executing function or method
For example, consider the following version of the VirtualZoo class from our zoo program. Notice that in this version, the VirtualPet object created in the VirtualZoo constructor is not assigned to a variable.
package { import flash.display.Sprite; import zoo.*; public class VirtualZoo extends Sprite { public function VirtualZoo () { new VirtualPet("Stan"); } } }
In the preceding code, when ...
Get Essential ActionScript 3.0 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.