title: Android Game Loop: How to reach an average of 60 FPS?
body: <p>I have seen the game Airplane Madness rendering the screen at a constant average of 60 FPS on my HTC Wildfire S. I've been trying to imitate the results on the same smartphone, but I just can't understand why my program only tops 15 FPS on average?</p>
  
  <p>How I can view the frame rate of Airplane Madness, you may ask? There's an option to toggle FPS counter in the Settings. I set mine to ENABLED.</p>
  
  <p>Here's my code. This is my game loop. There are some interesting results from this:</p>
  
  <pre><code>    public void run() {
          // Average FPS for Android is 15 on Test Phone is 13 through 15.
          // Max FPS is 20.
          // Occasional bursts of FPS60 may occur.
          long now, lastTime = System.nanoTime();
          double process = 0.0;
          final double NSperTick = 1000000000.0 / 60.0;
          while (running) {
              now = System.nanoTime();
              process += (now - lastTime) / NSperTick;
              lastTime = now;
              boolean ticked = false;
              while (process &gt;= 1) {
                  process -= 1;
                  tick();
                  ticked = true;
              }
              if (ticked) {
                  render();
              }
              swap();
              try {
                  Thread.sleep(2);
              }
              catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>
  
  <p><strong>Can anyone tell me what I'm doing wrong, what needs to be improved, etc., so that I can get an average of 60 FPS?</strong> This game loop, when running in Java (Not Android), it runs practically fine, so I'm not sure what logic parts are affecting the frame rate in my smartphone.</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>EDIT: Looks like I need to make all functions known to Man.</p>
  
  <pre><code>private void render() {
      synchronized (holder) {
          if (holder.getSurface().isValid()) {
              Canvas c = holder.lockCanvas();
              c.drawRGB(0x44, 0x55, 0xff);
              ball.render(c);
              holder.unlockCanvasAndPost(c);
          }
      }
  }
  
  private void swap() {
  }
  
  private void tick() {
      float[] values = {meter.X, meter.Y, meter.Z};
      ball.tick(values);
      handler.tick(ball);
  }
  </code></pre>
  
  <p>And here's how I check my game's average FPS:</p>
  
  <pre><code>public void run() {
      // Average FPS for Android is 15 on Test Phone is 13 through 15.
      // Max FPS is 20.
      // Occasional bursts of FPS60 may occur.
      long now, lastTime = System.nanoTime();
      int frames = 0;
      double process = 0.0;
      long frameTime = System.currentTimeMillis();
      final double NSperTick = 1000000000.0 / 60.0;
      while (running) {
          now = System.nanoTime();
          process += (now - lastTime) / NSperTick;
          lastTime = now;
          boolean ticked = false;
          while (process &gt;= 1) {
              process -= 1;
              tick();
              ticked = true;
          }
          if (ticked) {
              //render();
              frames++;
          }
          swap();
          try {
              Thread.sleep(2);
          }
          catch (InterruptedException e) {
              e.printStackTrace();
          }
          if (frameTime &lt;= System.currentTimeMillis() - 1000)
          {
              Log.d("FrameRate", Integer.toString(frames));
              frameTime += 1000;
              frames = 0;
          }
      }
  }
  </code></pre>
  
  <p>And here are the results from DDMS:</p>
  
  <pre><code>03-12 14:29:21.008: D/FrameRate(3533): 14
  03-12 14:29:22.061: D/FrameRate(3533): 15
  03-12 14:29:23.048: D/FrameRate(3533): 14
  03-12 14:29:24.033: D/FrameRate(3533): 14
  03-12 14:29:25.018: D/FrameRate(3533): 14
  03-12 14:29:26.000: D/FrameRate(3533): 14
  03-12 14:29:27.056: D/FrameRate(3533): 15
  03-12 14:29:28.047: D/FrameRate(3533): 14
  03-12 14:29:29.026: D/FrameRate(3533): 14
  03-12 14:29:29.995: D/FrameRate(3533): 14
  03-12 14:29:31.037: D/FrameRate(3533): 15
  03-12 14:29:32.015: D/FrameRate(3533): 14
  </code></pre>
  
  <p>EDIT 2: There's more info:</p>
  
  <p>I have commented out this code in the render() function:</p>
  
  <pre><code>synchronized (holder)
  </code></pre>
  
  <p>But still, I can only achieve 14 to 15 FPS. Is this a limitation I can't break through?</p>