[Aditya Kulkarni outlines some common memory problems that trip up fledgling developers, and explains how you can avoid them in this feature. Aditya is the Chief Lunatic at Loon Games Studio. All art work used in this blog is created and owned by the studio.]
From a sales point of view, your cool new mobile game should work on most of the devices used by people today. The term ‘most of the devices' evokes a ‘yikes' by a developer. As of Jan 2015, the number of unique mobile users was 3.6 Billion. It's the job of the developer to make the game/application work within the hardware constraints of a lot of those devices. Developers need to put a lot of thought while developing memory intensive software like games. By writing this article, I hope to help a non-tech person understand some of the memory problems that should be taken into account when developing 2D game projects.
1) Memory Leak: A memory leak can be defined as an allocated memory block which has not been released.
Analogy: Let's imagine a cabinet that you find in a locker room having a total of 3 perfectly square lockers.
Here, Danny would not be able to utilize the storage space that would have actually been available if John had removed the unused equipment from the locker.
2) Multiple instances of the same texture: A texture takes up width*height*4 (1 byte each for red, green, blue and alpha) of memory space. So an image of size 512x512 will take up 1.05 MB of memory. The same texture used 3 times would then take 3.15 MB of memory. So we would be using 3 times more memory if we used those many instances of the image as a texture.
Analogy: Consider a square sticker which shows your favourite character, ‘buttman ‘and can be reused for an infinite number of times.
You need to show this cool new character to 3 different people. So you can either:
Case 2 works for us since we can just reuse the same sticker multiple times. It actually works in parallel in case of a software program, so all of them can look at buttman at the exact same time. Magic, yeah.
3) Unnecessary Iterations: Calling a for/while loop once is easier on the resources than calling it multiple times. Batch multiple tasks in one loop if you can.
Analogy: You are a spy assigned to deliver packages to 3 different, far off locations in tropical heat. You have also been asked to get top secret info by a different boss from the same 3 locations above.
Since both the tasks have been assigned to you by different bosses, you can either:
You would be a damn stupid spy if your mission plan was Case 1 due to the simple fact that it takes a longer time and more resources to complete the same set of tasks.
4) Keeping unused objects in memory: At any given point in time, only a definite number of objects will be used and / or shown on the screen. Since we have limitations on the memory that can be used, any additional objects still in memory are a drain on resources and should be released.
Analogy: You need to push a cart from point A to point B. Let's assume that the task at hand would be easier if the load on the cart is non-existent or lighter at the very least. Since you love beer, you load up the cart with a barrel of beer anyway, which just makes the task at hand harder, you alcoholic nitwit.
So the best solution would be to not load up the barrel even if your emotions say otherwise.
The list of problems is definitely more than I could write about in a single article. As a developer, you should be able to identify these problems quickly and apply the needed solution. Shipping the product after overcoming these challenges is what makes game development exciting. Everyone has 24 hours in a day. For us, it's the idea of other human beings spending and enjoying some of those precious hours of their lives on something that we create, that pushes us to develop games for the populace to consume.