Skip to Main Content
Essential ActionScript 3.0
book

Essential ActionScript 3.0

by Colin Moock
June 2007
Intermediate to advanced content levelIntermediate to advanced
948 pages
27h 2m
English
O'Reilly Media, Inc.
Content preview from Essential ActionScript 3.0

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Essential ActionScript 2.0

Essential ActionScript 2.0

Colin Moock
The ActionScript 3.0 Quick Reference Guide

The ActionScript 3.0 Quick Reference Guide

Jen deHaan, David Stiller, Darren Richardson, Rich Shupe
ActionScript 3.0 Cookbook

ActionScript 3.0 Cookbook

Joey Lott, Darron Schall, Keith Peters

Publisher Resources

ISBN: 0596526946Errata