12
\$\begingroup\$

I've been studying opengl es and an example I saw was using a "Pool" class to keep track of touch and keyboard events.

Could someone please explain how and why a pool class is needed. From what I was reading it had something to do with garbage collection and limiting the number of input classes that are made.

This all seems a little abstract to me, so if someone could please explain what is going on, I'd appreciate it, I'll pasted some code here:

    public Pool(PoolObjectFactory < T > factory, int maxSize) {            
        this.factory = factory;         
        this.maxSize = maxSize;         
        this.freeObjects = new ArrayList < T > (maxSize);     
    }  
         
    public T newObject() {        
        T object = null ;        
        if (freeObjects.isEmpty())            
            object = factory.createObject();        
        else             
            object = freeObjects.remove(freeObjects.size() - 1);        
            return object;     
    } 

    public void free(T object) {         
        if (freeObjects.size() < maxSize)             
            freeObjects.add(object);     
    }

    PoolObjectFactory <TouchEvent> factory = new PoolObjectFactory <TouchEvent> () {
     
    @Override     
    public TouchEvent createObject() {         
         return new TouchEvent();     
    } 

    Pool <TouchEvent> touchEventPool = new Pool <TouchEvent> (factory, 50); 
    TouchEvent touchEvent = touchEventPool.newObject(); 
    . . . do something here . . . 
    touchEventPool.free(touchEvent);

Thanks!

\$\endgroup\$

1 Answer 1

17
\$\begingroup\$

Pools are used when the number of objects will fluctuate dramatically and are used to reduce the amount of memory allocation and garbage collecting.

Using a pool the standard new Object() which allocates new memory is replaced with pulling an already allocated object from the pool. This is much faster even if you go though and reset every variable in the old object to a default.

If you create a new item each time you have a huge over head as memory must be allocated for each object. Also because you are creating so many objects you must clean them up often which causes the garbage collector to be run very often further hurting performance.

A common example is bullets.

For a FPS there may be 0 bullets on screen then 1000 the next second and then 0 again the second after that. If you were to create a new bullet for every single one fired the constant memory allocation would be extremely performance intensive. In addition the garbage collector has to track all of these instances and clean the up periodically which takes even more time.

If you have a pool of 5000 bullets and only update and interact with the ones not in the freeObjects list you only have 5000 total allocations no matter how long the firefight lasts instead of 1 allocation per bullet. Also the garbage collector only needs to run once any possibility of a firefight has ended. Interacting with the memory is very slow so this makes a huge impact on performance and makes sure the workload is evenly distributed preventing frame rate stutters.

\$\endgroup\$
3
  • \$\begingroup\$ So I'm thinking a pool class for particle effects would be extremely essential, am I right? \$\endgroup\$ May 2, 2013 at 11:54
  • \$\begingroup\$ Yes that exactly the sort of situation pools were designed for. \$\endgroup\$ May 2, 2013 at 12:04
  • \$\begingroup\$ Is it better practice to have a class pool it's own objects or use a factory? I.e. Object.getObject() vs ObjectFactory.getObject() \$\endgroup\$
    – user14992
    May 25, 2013 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .