dcsimg
September 29, 2016
Hot Topics:

Top 10 Performance Boosters for Your Android Apps

  • March 30, 2015
  • By Vipul Patel
  • Send Email »
  • More Articles »

Introduction

Mobile devices do not have the luxury of processing power that desktop, laptops, and workstations have. Therefore, apps targeted for these platforms can benefit from certain performance considerations that can support the form factor they will be executed on.

Let us take a look at a few tips on how to boost performance of your Android apps.

Tip 1: Avoid Unnecessary Objects

It goes without saying that on memory- and CPU-constrained devices, the fewer items that the mobile OS has to deal with, the better. This implies that any unnecessary object creation should be avoided. Even though the garbage collector can help clean up unneeded objects, it is still an operation that takes CPU cycles and can potentially impact customer experience (especially if concurrent GC is not used, which can cause jitters).

For example, when the operation involves extracting a section of data from an input string, use a substring operation that avoids creating an entirely new object.

Each class instantiation can use up to 16 bytes of memory, and on memory constrained devices, you can use all you can have.

Tip 2: Static Over Virtual

Calls to static fields are up to 15-20% faster than calls to virtual fields. So, if your class is not going to be instantiated, consider masking its field as static to improve performance.

Additionally, if the values are not changed, mark them as "final" to tag then as constants.

For example:

// this value will not change
static final int val = 100;

Tip 3: Forget Getters and Setters for Privates

Remember what we learned in CS101 classes about good programming practices dictate providing getters and setters? This does not apply for Android devices because virtual method calls are expensive, even more than instance field lookups. It is okay to provide getters and setters for public fields, but not for private.

The improvement is to the tune of 3x to 7x reduction in "call expenses."

Tip 4: Use Enhanced for Loops

We all love for loops. But, the enhanced for loop (affectionately referred to as a for-each loop) is fastest even for devices without JIT.

Consider these two implementations:

public void one() {
   int sum = 0;
   int[] localArray = myArray;
   int length = localArray.length;

   for (int i = 0; i < length; ++i) {
      sum += localArray[i].value;
   }
}

public void better() {
   int sum = 0;
   for (int a : myArray) {
      sum += a.value;
   }
}

The better() method is indeed better even though they do the same thing....

Tip 5: Release Memory When the UI Is Hidden and When Available Memory Dips

When you application is pushed to the background, release all memory. This will enable memory to be available to the foreground app.

Additionally, when available memory dips, have logic in your code to clean up memory that you do not need (by inducing garbage collection).

Tip 6: Be Smart About Using Services

If you need to use Services to do work, stop the service if it is not actively doing work. This is because when a service is running, the process for that service is kept alive, making less memory being available for other operations.

If you need to use Services, use IntentService, which will automatically finish itself when the allocated work is complete.

Tip 7: Bitmaps Are Memory Hogs

Bitmaps are heavy on memory use. If your app loads bitmaps, choose a size that match the resolution of your app. This will help keep the memory consumed by the bitmap loading code to the minimum needed to render a complete experience on your app. If you are running into memory issues and you can afford a degraded user experience, you can even use lower resolution bitmaps for rendering purposes.

Tip 8: Use ProGuard to Strip Out Unnecessary Code

There is a tool available for Android developers, called ProGuard, that shrinks, optimizes, and even obfuscates code. This will help make your code more companionable and in turn will require fewer RAM pages to be mapped into memory.

Tip 9: Choose Double Over Float; Avoid Both if Possible

Both double and float are two times slower than int, so avoid using them if at all possible. If you need to use them and space is not a concern, choose double over float.

Tip 10: Do You Really Need Dependency Injection Frameworks?

For production applications, avoid dependency injection frameworks unless you have a business reason to use them. They lower the performance of an application by the inherent nature of what they are trying to achieve.

Summary

In this article, we learned about a few performance boosting tips to incorporate in your Android applications. I hope you have found this information useful.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.


Tags: Dependency injection, Android, services, android apps, garbage collector, for loop




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel