8
\$\begingroup\$

I'm developing a android game and I have a question about storing the game elements without firing garbage collector.

My game needs a collection where the game elements are stored according to x,y positions (all game elements have x,y,width and height). The collection is then called every frame to retrieve the elements according to cameraX, cameraY, camera width and height (user can scroll around the game).

Example:

 function draw() {
   tmp = collection.getElements(tmp,cameraX,cameraY,cameraWidth,cameraHeight);
     for(int i = 0; i < tmp.size(); i++) {
        tmp.get(i).draw();
     } 
  }

I'm currently using the Vector class to represent the collection elements, but the gc keeps firing every couple of minutes. I do all my allocations up front. I've also modified the getElements function to accept 1 more parameter - a temp vector(allocated upfront) which is filled with elements and then returned.

How should I store the game elements so the gc wont fire (I prefer never if possible)?

I also add elements to the collection during runtime, do I have to allocate them upfront also?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Yes, if you allocate anything at runtime, it will eventually trigger the GC. Generally, you get around this by implementing an object pool ... that way, you can preallocate (or at least collect new instances for reuse) all objects like bullets and other entities to avoid triggering the GC.

edit: You also might want to use a memory profiler (like the eclipse memory analyzer) which can help you figure out what routine is generating objects at runtime.

\$\endgroup\$
2
  • \$\begingroup\$ so insted of using a Vector class use MyObject[]? But then i have to preallocate the object pool size so i can dynamicly add and remove objects... something like MyObject[] pool = new MyObject[MAX_OBJECTS]; is this what u meant? \$\endgroup\$
    – Jernej
    Jun 7, 2011 at 15:58
  • \$\begingroup\$ more specifically, if you want to 100% avoid new object allocations, then you need to allocate each instance contained in that array. I wrote a class in C# for the XNA framework that strikes a nice balance ... it only creates new instances when the pool does not have an existing instance to give. The result is that the first few seconds might allocate a few objects, but once we reach the max concurrent # of instances, no new allocations occur. Should be relatively easy to convert to java: codecube.net/2010/01/xna-resource-pool \$\endgroup\$ Jun 7, 2011 at 16:33
2
\$\begingroup\$

I also suggest reading through the source code for Replica Island, which was developed by an Android engineer. He has a generic object pool class which can be extended for pools of custom objects. He also has a custom array class (with add/delete/search capabilities) to avoid having to use an iterator when traversing through an array of objects.

Also would like to mention that the DDMS on the Android SDK in Eclipse can track memory allocations (see Allocation Tracker tab in the image).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .