Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).


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

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?

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
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? – Jernej Jun 7 '11 at 15:58
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: – Joel Martinez Jun 7 '11 at 16:33

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.