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

Freeing Memory Used by Bitmaps

Every pixel in every BitmapData object occupies a little bit of system memory—four bytes to be precise (one byte for each color channel). Though the memory used by each pixel is insignificant on its own, pixels can collectively add up to form a significant memory demand. Hence, to reduce the amount of memory required by the Flash runtime when working with bitmaps, every ActionScript program should ensure that all BitmapData objects become eligible for garbage collection when they are no longer required.

To make a given BitmapData object eligible for garbage collection, either remove all references to it or remove all references to objects that reference it. Either way, the memory used by the BitmapData object will automatically be freed by the garbage collector in a future mark-and-sweep cycle.

To immediately free the memory occupied by the pixels of the BitmapData object (rather than waiting for the garbage collector to free it), use the BitmapData class's instance method dispose( ). For example, the following code creates a BitmapData object that consumes 1600 bytes (20 pixels wide × 20 pixels high × 4 bytes per pixel):

var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF00FF00);

To immediately free the 1600 bytes of memory, we use dispose( ), as follows:

imgData.dispose();  // Free memory used by imgData

The dispose( ) method frees the memory consumed by imgData's pixel information, but does not immediately free the memory consumed by the imgData ...

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