SurfaceView FPS Issue

Problems with Canvas, OpenGL, etc...

SurfaceView FPS Issue

Postby serious7 » Sun May 22, 2011 3:27 pm

Hi,

I am new to the android world (new to any mobile platform to be specific). I have a problem with SurfaceView. I have a thread set up as my game loop which locks the canvas, prints out a black screen and the current fps, and prints out a red screen in case the fps goes below 25. The thread then unlocks and posts this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void run() {
  2.  
  3.              //UPDATE
  4.              while (mRun) {
  5.          
  6.              Canvas c = null;
  7.              try {
  8.                    c = mHolder.lockCanvas(null);
  9.                    synchronized (mHolder) {
  10.                         mPanel.doDraw(mElapsed, c);            
  11.                    }
  12.              } finally {
  13.                    
  14.                  if (c != null) {
  15.                          mHolder.unlockCanvasAndPost(c);                   
  16.                  }
  17.  
  18.              mElapsed = System.currentTimeMillis() - mStartTime;
  19.              mStartTime = System.currentTimeMillis();
  20.          
  21.              }
  22.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I have a sony x10 and just outputing a "black screen" gives me an fps of 33 solid. The issue is why is the canvas SO slow because all it does is output a black screen?

My second question (which is a more serious one), the fps "jumps" down to 15 every 3-5 seconds on my x10 with just that code. This is a very bad performance issue because as I work on my app, there will be constant quirks of lag later. When I ran this code on motorola atrix, the fps was solid 60 and the fps only jumped below 25 fps every 20 seconds. That phone is obviouslly a faster phone, but I don't get it. Why is the FPS jumping like that? My ultimate goal is to make a 2-d game.
serious7
Freshman
Freshman
 
Posts: 2
Joined: Sun May 22, 2011 3:15 pm

Top

Re: SurfaceView FPS Issue

Postby Sweep88 » Mon May 23, 2011 2:05 pm

I bet its because of the Garbage Collector, look at your logcat output to determine if there ist any output like "GC_FOR_MALLOC" or "GC_EXPLICIT". If this is the case you have to profile your application and check which objects are created and destroyed in your drawing code. Its never good to have any "new CLASSNAME(...)" parts in your drawing method. Instead of that you should create your objects one time, and then reuse them!
Sweep88
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 12, 2011 3:23 pm
Location: Germany

Re: SurfaceView FPS Issue

Postby serious7 » Wed May 25, 2011 1:46 am

Thanks for your advice. Funny thing is now its not showing the red screen often. But you were right it may be garbage collection. I watched log cat and here is what it showed.

dalvikm GC freed 13183 objects/512648 bytes in 70 ms. After that the fps half of the time jumps down to 15 fps. Otherwise now its steady 30 :). Is this error message a very bad one?
serious7
Freshman
Freshman
 
Posts: 2
Joined: Sun May 22, 2011 3:15 pm

Re: SurfaceView FPS Issue

Postby Sweep88 » Wed May 25, 2011 8:49 am

Not really a bad one, but you should try to avoid it. Simplest way is to create as less objects as possible in your render loop, in my case i use a lot of "Matrix" und "Vector" objects in my render loop, so i wrote a Manager Class which holds for example 50 Matrix objects and 50 Vector objects, and instead of creating new Objects if needed i ask my Manager class to give me an unused object and use this one, this avoids the garbage collector to work, because all objects are referenced and used.
There are also a lot of build in Object creations which are invisible for you like this construct:
Code: Select all
for (Object o : objectList) {}

This creates an iterator, instead of using this construct you should use this:
Code: Select all
for (int i = 0; i < objectList.size(); i++) {Object o = objectList.get(i);}


But there is much more to mind. To find out which objects you create use DDMS, the Eclipse Plugin has an Build-In DDMS view, just switch to it, choose the tab "Allocation Tracker", mark your prozess and click on "Start tracking", and a little time later on "Get Allocations". This will help you!

Good Luck!
Sweep88
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 12, 2011 3:23 pm
Location: Germany

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests