Max Size

With this object pooling, should you always have a max size, never, or is there some sort of guideline when to do it and how big to make it?

The constructor requires a maxsize, though unlike Arrays, this memory isn’t allocated, it’s simply used by the ObjectPool to decide whether or not to destroy released items.

You will always get an item from the ObjectPool whether you’ve reached the max or not when you request one. The max size relates to how many inactive objects will stay in the pool.

In terms of what that max size should be, there’s no one solid answer to that question. The best answer is to calculate the rate that you might be instantiating the object (say your gun is capable of firing 10 rounds per second) and the average amount of time that object will be alive (say it takes 2 seconds to travel across the scene). That particular combination would mean that there would generally be no more than 20 (10/second * 2 seconds) bullets on the screen at any one time. This would mean that a max pool size of 20 should ensure that you never have to destroy any bullets.

Of course, it could be more complex than that. If each enemy is capable of firing 10 bullets per second, then you would also need to factor in the maximum number of enemies (or create an ObjectPool for each enemy).

If the goal is to never destroy any bullets, wouldn’t infinity maxsize work just as well?

Sure. At that point, it really wouldn’t matter… at a certain point, you’ll reach the equilibrium point, where no new bullets are created because they’re recycling at the same rate they’re firing.

Privacy & Terms