Corona SDK: Improve your game performances with Object Pooling

Performances have always been a recurring problem in game development. It’s true that the newest devices have so much juice that you could even ignore most optimization tricks (especially if supporting older/lower end devices is not in your plans), but there’s one technique that should always be followed, even if it all seems to run fine: Object Pooling.

Say it again?

Object Pooling. It consists in avoiding continuos creation and destruction of objects, by recycling the already existing ones. This produces several benefits that no one should pass on. First of all, there’s less “garbage” that the LUA garbage collector must take care of. This means reducing sudden FPS drops for apparently no reason. If you don’t use spritesheets for that specific object, you will also avoid the texture loading time that could occur when creating a new object that doesn’t have the image already loaded in memory, which freezes your app for a couple of noticeable milliseconds. It’s a quick and painless process that should be used in all the required situations.

So I should ALWAYS use it?

Wait. I said required situations. If you’re going to spawn just 2-3 copies of the same object, it might really not be worth it. Object Pooling becomes necessary when you have a ton of objects that you keep spawning in your game. Take for example bullets, and think of how many bullets will be shot in a generic shooting game. What about enemies in an Endless Runner game, that keep on spawning on top of platforms until it’s game over? These are the kind of cases where you should consider implementing an Object Pooling method.
Fortunately enough, it’s not particularly hard to do so. All you need is a “create” function, which will store the code used to create the object you wish to put in a pool, an “activate” function, which is called when you take the object from the pool and spawn it on your screen, and a “deactivate” function, used to get rid of an object on the screen by putting it back in the pool, ready to be used again. Oh, and don’t forget our Object Pooling code snippet, which will do all the dirty work for you.

How does it work?

The first thing you should do, is saving the above code into a LUA file called ragdogLib.lua. Next, we’re going to create a pool for several bullets that will be spawned on the screen when the user taps it. The bullets have different colors so you can see the recycling at work. Once spawned in the center of the screen, they will move towards the tap coordinates, while fading out until they completely disappear, ready to be used again.

As you can see, we call the initObjectPool function, passing it the number of objects we want in it, and a table that contains the 3 functions we already mentioned (create, activate and deactivate), plus an enterFrame function that will be used by all our objects. You can set any kind of additional function, it will always be automatically added to all the created objects. It’s important that the first argument of each function is “self”. Also note that you can completely remove a pool with the pool:removeSelf() function.

That’s it. By following the example you can increase your performances even in particularly intense scenes, without compromising on flexibility.


  1. I try to use this tutorial to reduce the memory usage of my game. I use storyboard and instead of using physics with setLinearVelocity to move my objects, I use with a collision detection using contentBounds to detect if some rectangles overlap each other.

    My problem is that with this code, I can only detect the first bullet created in the whole pool and I can’t figure out how to detect all the bullets in the pool.

    • Hi Pat,
      from what you described, it seems that you’re only applying the function to the topmost object of the pool.
      We would need to check your code to see what might specifically be wrong with it. If you can send a sample to our support (or paste it here) we’ll be glad to help out (: