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

Incremental Mark and Sweep

In the preceding section, we learned that an object becomes eligible for garbage collection (automatic removal from memory) when it is unreachable. But we didn't learn exactly when unreachable objects are removed from memory. In an ideal world, objects would be removed from memory immediately upon becoming unreachable. However, in practice, removing unreachable objects immediately would be very time consuming and would cause most nontrivial programs to run slowly or become unresponsive. Accordingly, ActionScript does not remove unreachable objects from memory immediately. Instead, it checks for and removes unreachable objects only periodically, during garbage collection cycles.

ActionScript's unreachable-object removal strategy is known as incremental mark and sweep garbage collection. Here's how it works: when the Flash runtime starts, it asks the operating system to set aside, or allocate, an arbitrary amount of memory in which to store the objects of the currently running program. As the program runs, it accumulates objects in memory. At any given time, some of the program's objects will be reachable, and others might not be reachable. If the program creates enough objects, ActionScript will eventually decide to perform a garbage collection cycle. During the cycle, all objects in memory are audited for "reachability." All reachable objects are said to be marked for keeping, and all unreachable objects are said to be swept (removed) from memory. However, ...

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