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: Actionscript, Free code, Object pool

10 Comments add your own
hi Richard,
it is strange idea for me. if you can put an example for that it will be great.
regards
delizade | 14th October 2008 at 22:08
Hmm, very interesting! Too bad I’m already 80% of the way through a project where this could be really useful; a little late to do too much with it now. In the future, though!
One question: I noticed that in the construct method, you’re doing a big switch to check how many arguments you have coming in. Is there any way to use apply with a constructor? Might be more flexible, but maybe that doesn’t work.
Dru Kepple | 17th October 2008 at 00:33
Unfortunately there’s no way to use apply with a constructor.
richard | 17th October 2008 at 08:26
I use Objects pools too, but I implemented it in a different way. I putted the pool in the created object. This way you can use the constructor. I use linked stack for the pool. This can be added to any class with little changes.
package{ public class Poolable{ public var userData:*; private var next:Poolable; private static var first:Poolable; public function Poolable(){ trace("object creation!"); } public function recycle():void{ if(first==null){ first = this; }else{ this.next = first; first = this; } } public static function getObject(data:*):Poolable{ var poolable:Poolable; if(first==null){ poolable = new Poolable(); }else{ poolable = first; first = first.next; } poolable.userData = data; return poolable; } } }Frederic Gascon | 2nd December 2008 at 05:26
Bummer about apply not working with constructors, I wonder what they were thinking?
I decided to adapt your construct class and use the ..rest property instead.
e.g.
http://code.google.com/p/troyworks/source/browse/trunk/AS3/dev/src/com/troyworks/util/new2.as
TroyWorks | 5th December 2008 at 04:57
yes an example would be nice because I just don’t see how it functions…but the idea is good!!
Ben | 10th March 2009 at 19:34
This object pool pattern is very straightforward, not much variation in implementation. It’s a good example.
Minh Ha | 11th March 2009 at 09:35
This is an interesting idea, but does it worry anyone else about being a potentially huge memory leak? I’m thinking EJB when I hear object pools which regulate the number of pooled objects. That would be a nice addition to this.
David Lance | 1st April 2009 at 22:13
It’s very nice. however, if I call disposeObject many times to an object, this class pushes the same object to the pool. it could be problem. so I check whether the object which will be disposed is in the pool or not. What do you think about it? very thanks.
Jack Koo | 18th August 2010 at 07:07
[...] http://www.richardlord.net/blog/object-pool-class [...]
?ZZ?AS3 ??? ?? | ??? ? blog | 22nd August 2010 at 13:59
Leave a Comment comment policy
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