Archive for October, 2011

Simple Generic Object Pooling in AS3

For Flash and Flex, object pooling is very important for performance, also an efficient way to avoid memory leak. Although many developers should have found good way to implement pooling, I unable to google a good one fitting my needs. So here’s my simple solution.

You could find source code here: https://github.com/neatfilm/neatfilm.com-open-source-code/tree/master/com/neatfilm/framework/view

Basically, a pool could store any kinds of object (maybe sprite, UIComponent), as long as it implements IReusable interface.

  • function cloneNewObject():IReusable;
  • function reset():void;

Basically, the pool will be set with a single object as seed (also first object), any new object will be made by cloneNewObject() function, and stored in a single array. You can put object back, and first not-in-use object in the array will be picked when asking.

Outside of the object pool, you may set one or multiple array or/and dictionary to keep references of these objects for different purpose if necessary.