I made a simple game for Android devices last month and learned first hand just how easy it is to use up the heap memory assigned to an app on Android and ended up with a functional game that calls the garbage collector far too often for my liking, resulting in annoying stutters about once per second.
To combat these memory issues I’ve been researching and implementing a generic object pool. It adds some abstract mess to my code, but its been super helpful in dealing with my memory niggles. I’ve run into a bit of an issue though and I’ve been failing to find information on this topic so I’d like to get some opinions here.
The object pools I’ve been coding have been working great for classes I’ve personally coded (Polygon, Vectors, game objects, etc.), but what about the memory allocations of external classes? Mainly I’m curious about how high performance Android games deal with the memory allocations of arrays and arraylists. If I create an arraylist of objects in a class that’s run on each loop of the core game loop, I can easily reuse the memory of the objects this arraylist holds by handling their creation through an object pool and then recycling them into the pool once each object is no longer in use, but this leaves me with the memory overhead of the empty arraylist which was never recycled. What can I do with this? Create a separate object pool for the arraylist object to be recycled each frame?
Similarly, what can I do about a situation where I need to create some arrays holding primitives (lets say a float[] array) very frequently. Is there some means of clearing the memory of these arrays, in maybe another object pool? Maybe I just need pools everywhere…
If the question is unclear, I apologize. It’s 3AM and I’m quite tired.
I’ll check back tomorrow and can post sources of my current object pool implementation if the question seems unclear. I tried pooling arraylists but ended up creating my own memory leaks somehow, so I feel I’m not on the right track yet.