Screenwriter, Choreographer, Game Developer

Object Pool class

Posted on

Two of the slow operations in the Flash player are object creation and garbage collection. If we pool objects - save objects when they're no longer needed and reuse them later when another object of the same type is required - then object creation is kept to a minimum and garbage collection is reduced to zero.

Joa Ebert discussed this in his presentation at Flash on the Beach. I already use specific object pooling in Flint particles, but while Joa was talking I had an idea and wrote a class for generic object pooling of all object types. I finally had time to test the class, and it works as expected, so I've added it to my open code repository.

The object pool class manages pooling of all objects through two simple methods. You can obtain an object from it like this

var obj:SomeClass = ObjectPool.getObject( SomeClass );

If there are any such objects in the pool, one of them will be returned. If there are no such objects in the pool, a new one will be created and returned to you.

When you no longer need an object, you can add it to the pool for later reuse like this

ObjectPool.disposeObject( obj );

The object doesn't have to have been created through the pool - any object can be dropped into the pool with the disposeObject method.

The source code is available in my GitHub code repository.

Share this post or a comment online -


Also in the collection Actionscript