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

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 ...

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