title: Android Memory Leak in ArrayList
body: <p>I am developing an Android game.  As the game progresses it gets steadily slower.  I am sure some of the speed changes are due to the higher enemy count and stuff like that, but I am also worried that I may have a memory leak. </p>
  
  <p>The nature of the game is this:</p>
  
  <p>It is a tower defense game built with lists and arrays.  All of the tower data is stored in numerous gameBoard arrays.  Throughout the drawing and update code every block is evaluated.  Most times nothing needs to be done.  The game is a 30x14 grid, so 420 locations are evaluated probably 15 times throughout the course of the loop.  This isn't great programming, but I assumed the code was simple enough that it would work fine.  And most times nothing needs to be done, so I didn't think it would matter.</p>
  
  <p>The enemies, all bullets and various particle effects are all stored and updated in lists like the code seen below.  I skinned the game a couple days ago, keeping all of the game logic the same, but adding effects. Adding bitmaps and coloring styles did not change the way the game ran.  The slowdown was apparent when I added particle effects like those shown below.  They do not cause any slowdown early in the game.  Later in the game they do.  I assumed that the memory was being managed wrong because of this. </p>
  
  <p>I have a bunch of different classes like this: </p>
  
  <pre><code>public class FadeBlock {
      private float x;
      private float y;
      private float radius;
      private int level;
      private float startRadius;
      private float endRadius;
      private float elapsedTime;
      private float fadeTime;
      private boolean terminate;
      private float percent;
  
      public FadeBlock(int Level, float CenterX, float CenterY, float StartRadius, float EndRadius, float FadeTime)
      {
          x = CenterX;
          y = CenterY;
          level = Level;
          fadeTime = FadeTime;
          terminate = false;
          percent = 1;
          startRadius = StartRadius;
          endRadius = EndRadius;
          radius = startRadius;
      }
  
      public void Update(float ElapsedTime)
      {
          elapsedTime += ElapsedTime;
          percent = (((float) elapsedTime) / ((float)fadeTime));
          radius = (endRadius - startRadius)*percent + startRadius;
  
          if (percent &gt;= 1)
          {
              terminate = true;
          }
      }
  
      public void Draw(Canvas canvas, Paint paint)
      {
          if (!terminate)
          {
              switch (level) {
              case 1:
                  paint.setARGB(255, 61, 190, 255);
                  break;
              case 2:
                  paint.setARGB(255, 162, 0, 194);
                  break;
              case 3:
                  paint.setARGB(255, 0, 194, 162);
                  break;
              case 4:
                  paint.setARGB(255, 129, 194, 0);
                  break;
              case 5:
                  paint.setARGB(255, 255, 85, 0);
                  break;
              case 6:
                  paint.setARGB(255, 194, 65, 0);
                  break;
              default:
                  paint.setARGB(255, 61, 190, 255);
                  break;
              }
              paint.setAlpha((int) (255*(1 - percent)));
  
              canvas.drawRect(x-radius, y - radius, x + radius, y + radius, paint);
          }
      }
  
      public boolean Kill()
      {
          return terminate;
      }
  
  }
  </code></pre>
  
  <p>That I add to an arrayList like this:</p>
  
  <pre><code>public List&lt;FadeBlock&gt; FadingBlocks = new ArrayList&lt;FadeBlock&gt;();
  </code></pre>
  
  <p>And manage with the following loops:</p>
  
  <pre><code>for (int i = 0; i &lt; FadingBlocks.size(); i++)
  {
      FadingBlocks.get(i).Draw(canvas, paint);
  }
  
  for (int i = 0; i &lt; FadingBlocks.size(); i++)
  {
      FadingBlocks.get(i).Update(elapsedTime);
  }
  
  for (int i = (FadingBlocks.size() - 1); i &gt; -1 ; i-- )
  {   
      if (FadingBlocks.get(i).Kill())
  {                       
          FadingBlocks.remove(i);
      }
  }
  </code></pre>
  
  <p>To add items to the list I do something like this: </p>
  
  <pre><code>FadeBlock tempBlock = new FadeBlock(Units.get(i).getLevel(), Units.get(i).getX()+16, Units.get(i).getY()+16, 16, 32, 250)                   
  FadingBlocks.add(tempBlock);
  </code></pre>
  
  <p>Is that a bad way to manage my objects?  Would that cause memory issues over time on an android device?</p>