title: How can I avoid garbage collection delays in Java games? (Best Practices)
body: <p>I'm performance tuning interactive games in Java for the Android platform.  Once in a while there is a hiccup in drawing and interaction for garbage collection.  Usually it's less than one tenth of a second, but sometimes it can be as large as 200ms on very slow devices.</p>
  
  <p>I am using the ddms profiler (part of the Android SDK) to search out where my memory allocations come from and excise them from my inner drawing and logic loops.  </p>
  
  <p>The worst offender had been short loops done like,</p>
  
  <pre><code>for(GameObject gob : interactiveObjects)
      gob.onDraw(canvas);
  </code></pre>
  
  <p>where every single time the loop was executed there was an <code>iterator</code> allocated.  I'm using arrays (<code>ArrayList</code>) for my objects now.  If I ever want trees or hashes in an inner loop I know that I need to be careful or even reimplement them instead of using the Java Collections framework since I can't afford the extra garbage collection.  That may come up when I'm looking at priority queues.</p>
  
  <p>I also have trouble where I want to display scores and progress using <code>Canvas.drawText</code>.  This is bad,</p>
  
  <pre><code>canvas.drawText("Your score is: " + Score.points, x, y, paint);
  </code></pre>
  
  <p>because <code>Strings</code>, <code>char</code> arrays and <code>StringBuffers</code> will be allocated all over to make it work.  If you have a few text display items and run the frame 60 times a second that begins to add up and will increase your garbage collection hiccups.  I think the best choice here is to keep <code>char[]</code> arrays and decode your <code>int</code> or <code>double</code> manually into it and concatenate strings onto the beginning and end.  I'd like to hear if there's something cleaner.</p>
  
  <p>I know there must be others out there dealing with this.  How do you handle it and what are the pitfalls and best practices you've discovered to run interactively on Java or Android?  These gc issues are enough to make me miss manual memory management, but not very much.</p>