Skip to content

Memory Management

WingEraser edited this page Feb 15, 2014 · 4 revisions

Flixel encourages recycling the sprites to avoid too much memory usage. If you don’t recycle them, each newly created object will consume more space in memory and you expect to have slowdowns after some amount of time and on mobile devices it will crash. Using the recycling technique will save you from null pointers and waiting for the system to garbage collect the things you don’t need.

##Object Pool and Recycling Flixel comes with handy recycling functions. There can be found in FlxGroup class. With FlxGroup you can easily grab objects that are alive, available, exists, etc. Let’s take a look how to use those handy functions.

getFirstAlive() Get the first object with alive is true.
getFirstAvailable() Get the first object with exist is false.
getFirstDead() Get the first object with alive is false.
getFirstExtant() Get the first object with exist is true.
getFirstNull() Get the first index that is null.
recycle() Get the first available object if it’s not available then create a new instance of that object.

###getFirstAvailable() Let’s create a bunch of bullets.

FlxGroup bullets = new FlxGroup();
for(int i = 0; i < 5; i++)
{
	bullets.add(new Bullet());
}
bullets.kill();

The Bullet got killed after they got created so that they won’t appear on screen. Now let the Player shoot a Bullet.

Bullet bullet = (Bullet) bullets.getFirstAvailable();
if(bullet != null)
{
	bullet.shoot();
}

Maybe you’re wondering why we need to check whether bullet is null or not, that’s because getFirstAvailable() only returns an object if exists is false. If there are 5 bullets on the screen, it means no bullets are available. As you can see no new objects are created, they got reused.

recycle()

The recycle() method looks similar to the others, but with recycle() you don’t have to create a bunch of object, it will create an object when it’s needed. When FlxGroup got a maximum size assigned and recycle() hits the maximum, then it will acts like getFirstAvailable() and thus you must check if the returned object is a null or not. If there is no maximum size specified then the group will create a new object if there is no object available. The array will grow in this way. For mobile devices it’s recommended to create objects during the initializing instead of runtime.

##Texture Memory While the most phones and tablets nowadays can handle 2048 x 2048 atlas sizes, it’s still recommended to create a smaller sheet of 2 x 1024 x 1024 for supporting older devices. If you don’t need the texture atlas anymore, you can dispose it via FlxAssetManager (FlxG._cache).

##Alternative Classes Use the math functions from FlxU instead of Java’s Math API.

For collections like ArrayList and Hashmap use the optimized version from libgdx which are Array and ObjectMap. In the com.badlogic.gdx.utils you can find more Map classes.

##Managing Audio Sound effects for an app should almost always be preloaded, e.g. before a level begins. Create a static FlxSound object and load it in there so you can reuse it anytime. If you don’t need sounds anymore, be sure to dispose them.