« | »

Object Pool class

8th October 2008

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 the Flint particles library, but while he 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, where you can download it.

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 google code repository.

Tags: , ,  

10 Comments add your own

Leave a Comment comment policy

required

required, hidden

XHTML: you can use these tags - <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to the comments via RSS Feed