Building an AS3 component framework: Garbage Management

Garbage Collection is a housekeeping function performed by the Flash Player. It cleans up and removes any objects or data from memory that are no longer being used by the Flash movie. ‘Garbage Management’ is my way of helping the Flash Player garbage collector do its job.

The Problem
As you may already know AS3 code is processed by its own just-in-time compiler AVM2 (Actionscript Virtual Machine 2). Destroying anything in AVM2 is notoriously difficult. Unlike in previous versions of the language (processed by AVM1) deleting an object does not necessarily remove it from memory. The AVM2 garbage collector will not remove any object that is still being referenced by anything else. This means you need to remove any outside references and de-register all event listeners to a class when it is no longer required.

For a good overview of the problems and some recommended management strategies take a look at Grant Skinner’s articles on garbage collection and resource management in Flash Player 9. If after this you want to get more technical then check out Garbage Collection and Memory Leaks by Adobe engineer Alex Hurai for a full low level explanation of how the garbage collector works.

The Solution
Garbage management is a ‘best practice’ rather than a specific implementation. You just have to get into the way of clearing up after yourself. Early versions of Flash Player 9 did not allow garbage collection to be directly invoked from code but since the release of update 3 you can force it by calling flash.system.System.gc(). I haven’t tested this myself yet but according to the boffins at CraftyMind you’ll need to call this twice due to the ‘mark and sweep’ nature of the Garbage Collector.

One good practice to get into is to always provide a destroy() function alongside your constructor. In this function you should remove all listeners and null any references to outside objects in your class. Note that due to ECMA262 compliance you can only delete dynamic properties. Cleaning up a class or Display object before removal becomes much easier when all you have to do is call a single standard (polymorphic) function before removing it from the stage.

Another good habit to get into is to use weak references on your Dictionary objects and listeners. It takes a little bit more code to write but it’s a good safety net should you forget to call destroy() before removing the class. Again Grant Skinner explains it best in his post AS3: Weakly Referenced Listeners.

The Benefits
A proactive approach to garbage management will help prevent memory leaks and improve the overall performance and saleability of your application.

The Code

package org.computus.core
{
  import flash.display.MovieClip;
 
  public class AbstractComponent extends MovieClip
  {
 
  // ------------------------------------------
  // CONSTRUCTOR
 
    public function AbstractComponent():void
    {
      super();
      init();
    }
 
  // ------------------------------------------
  // MEMORY MANAGEMENT
 
    protected function init():void
    {
      // concrete classes that override this function should call super.init()
      // initialise component here.
    }
 
    public function destroy():void
    {
      // concrete classes that override this function should call super.destroy()
      // remove all listeners here
    }
 
  }
}

Leave a Reply