2010-12-15

Optimizing code

Since mobile devices are pretty limited when it comes to hardware specs, there are a few things you should think of as a developer to optimize the code. This will make the app run faster and smoother, and the user will more likely keep using your app.

1. One thing I noticed when debugging my app, was a lot of lines in Logcat that looked like: "Gc freed x objects / y bytes in z ms". This is the garbage collector doing it's work and it's of course impossible to write an app that has no GC at some point. But if you start seeing these lines frequently and when the app is idle, you should be able to optimize the code.

The most probable cause for this is that you create too many objects. Look for code like:
public void onClick(View view) {
  String aString = object.getString();
  Integer anInteger = object.getInteger();
}
Change this by declaring the variables as class member variables instead, then you will create only one object instead of one object/onclick action. Note that you shouldn't use this as a general rule though. A member (global) variable's life cycle is longer compared to local variables, so it will allocate memory for a longer time.

In a GPS listener method executing once every second you should use member variables.

In a method that will be executed a few times only, local variables are preferred.

2. Analyze the layout. If you start nesting several LinearLayout you should consider switching to RelativeLayout instead. The deeper the layout tree becomes, the more expensive it is. One way to analyze this is to use the tool Hierarchy Viewer (included in the SDK). Run your application and click on <Focused Window> and then hit Ctrl + L to load the current activity. You should aim for a wide layout instead of deep layout with many levels.

3. Avoid boxing and Un-boxing of objects:
public void stupid() {
  Integer anInteger = Integer.valueOf(new Float(String.valueOf(1.1f)).toString());
}
This will create many objects. Use the primitives (i.e. float, int) instead of their wrapper classes (i.e. Float, Integer) when possible.

4. Access the member variables directly instead of using a getter method. It will always be faster.

5. Reuse views. For example, instead of creating a new TextView object for each TextView in a layout, you should consider reusing only one TextView.

6. This is more of a power consumption optimization, but a good one. If you are using GPS listener for example, you should consider turning it off when the phone is idle or when the user exits the activity where GPS is used, by implementing the onResume and onPause methods:
@Override
protected void onResume() {
  super.onResume();
  
  // Acquire a reference to the system Location Manager
  locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  // Define a listener that responds to location updates
  locationListener = new LocationListener() {
   @Override
   public void onLocationChanged(Location location) {
  .
  .
  .
  }
 // Register the listener with the Location Manager to receive location
 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
 protected void onPause() {
  super.onPause();
  locationManager.removeUpdates(locationListener);
  locationManager = null;
 }

Inga kommentarer:

Skicka en kommentar