Tuesday, 24 April 2012

Memory Optimisation and Refactoring

I can't find the person to attribute this to now but never a truer word was said when it was mentioned it took 4 hours to create the concept and then 4 months refining and finishing it. This is pretty much what happened to me with Astavoid. In December 2011 over the Christmas period I had pretty much got to grips with Corona SDK and its ease of creating Physics objects. However, from a reasonably simple concept the refactoring and attention to detail required was made more apparent the further I refined the concept. Before I go on further I wanted to point you all out to an excellent helper chart I found on the ansca forum around collision filters. This really helped me with optimising my physics objects but in the first instance actually explained to me how to make my whole game logic work. I would highly recommend you follow this link without delay, go now.

Told you it was good. So now you have bookmarked that page, printed off a few helper charts and worked out your physic logic you want to make sure when testing it not only works on the device in your hand but others who will inevitably download. I will talk about testing in my next post but what it really showed to me is just how important it is to not be lazy with your coding. I wouldn't say I was being lazy in the first instance but more a lack of understanding and this really came back to bite me. On newer devices such as the iPhone 4s (which I happen to have) it covers up all kind of sins and for that matter the Corona simulator isn't that reliable. So when testing on iPhone 3GS, for example, all kind of weird and wonderful things happen that I personally couldn't replicate.

I wouldn't be exaggerating if I said I rewrote the code of Astavoid a dozen times. This became hugely time consuming and while the concept of the game and most of the screens were finished I probably spent a very long two months on optimisation and performance issues. I would highly recommend you start by having a read of the performance and optimization tips described in this article. This got rid of some of the basic errors I was making.

In addition, to this was the concept of memory leaks and where to find them, again this article was incredibly helpful. Half the challenge of course is in finding the memory leaks and performance spikes. I didn't really get on with compiling out to Xcode simulator and using instruments as I found it too cumbersome. Instead I use a few community scripts and tools. The ones I would recommend are:

These tools were very helpful in at least identifying issues. The following links were things to try to eradicate them:

These are really useful resources that at least made me understand the importance of tidying up after yourself and not relying on the more up to date devices to do this for you. This was no more prevalent to my development than truly understand the memory utilisation and management of the storyboard api. I would strongly recommend you read the introductory explanation on the subject which cleared things up for me.

Finally, the biggest stabilisation effort I got was from dropping my FPS (frames per second) from 60 down to 30. I had originally drawn 250 animations for the rocket but found that on the older devices the extra texture memory used up and the amount of computations needed was making it struggle. So I dropped the frame rate, minimised my spritesheets and followed the optimisation techniques described and I found I had a smooth action game, that worked across multi device and operating systems.

 Hope this helps.

Astavoid is available on Google Play and iTunes


Post a comment