Friday, November 13, 2015

java - When and why is a Pool class needed to hold objects?


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 factory = new PoolObjectFactory () {

@Override
public TouchEvent createObject() {
return new TouchEvent();
}

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

touchEventPool.free(touchEvent);

Thanks!



Answer



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.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...