- Home /
When to use object pooling ?
Hi, I have a little confusion about usage of object pooling. Actually in some scenario I can not decide whether I should use object pooling or not. For example I have made a 2D game in which a 2D fruit object moves on screen and I have to hit it with arrow sprite and when they collides with each other they become destroyed and new object of fruit and arrow is created and game is infinity .So in this scenario I have pooled all the fruit objects and arrow objects but the point which is confusing me is that arrow and fruit stays alive for 2 to 5 seconds on screen and at a time its one fruit and one arrow on screen so what's the need of keeping memory. So what is better in this scenario object pooling or create destroy strategy? I am making this game for IOS and Android devices and one thing more that my objects have lot of initialization in start method.
don't optimize stuff unless there's a problem.
the scenario you're describing sounds pretty chill.
i wouldn't consider bringing in object pooling until you're churning through say more than 100 creates/destroys per second.
the places where i've seen object pooling have a real impact is when there's tens or hundreds of thousands of objects per second.
Answer by James2Games · Feb 09, 2017 at 05:36 AM
To quote a unity developer from a youtube tutorial on optimization.
"Use the profiler before you optimize"
If your game is running slow in the main menu but not in the game. Why optomize the game?
As @elenzil mentioned above. Object pooling is only useful when there are a lot of objects being created and destroyed and is causing heavy changes in the fps. Where as pooling a handful of small objects will speed up the game by maybe a few milliseconds but at the cost of keeping that memory always at bay and cluttering your code.
If you are unsure if you should pool something then most of the time it will be a no. If you think it should be pooled, then take a look at the profiler and see if there's much heavy usage coming from Instantiate or Destroy and make your decision there.