Android Performance Tips
Summary
I read bellow tips from Google site. These are not special tips but they are useful. When i started in Android, i didn’t read and follow them and my applications are really bad and slow. So please notice it.
This document primarily covers micro-optimizations that can improve overall app performance when combined, but it’s unlikely that these changes will result in dramatic performance effects. Choosing the right algorithms and data structures should always be your priority, but is outside the scope of this document. You should use the tips in this document as general coding practices that you can incorporate into your habits for general code efficiency.
There are two basic rules for writing efficient code:
- Don’t do work that you don’t need to do.
- Don’t allocate memory if you can avoid it.
One of the trickiest problems you’ll face when micro-optimizing an Android app is that your app is certain to be running on multiple types of hardware. Different versions of the VM running on different processors running at different speeds. It’s not even generally the case that you can simply say “device X is a factor F faster/slower than device Y”, and scale your results from one device to others.
To ensure your app performs well across a wide variety of devices, ensure your code is efficient at all levels and agressively optimize your performance.
Tip 1: Avoid Creating Unnecessary Objects
Object creation is never free. A generational garbage collector with per-thread allocation pools for temporary objects can make allocation cheaper, but allocating memory is always more expensive than not allocating memory.
As you allocate more objects in your app, you will force a periodic garbage collection, creating little “hiccups” in the user experience. The concurrent garbage collector introduced in Android 2.3 helps, but unnecessary work should always be avoided.
Thus, you should avoid creating object instances you don’t need to. Some examples of things that can help:
- If you have a method returning a string, and you know that its result will always be appended to a
StringBuffer
anyway, change your signature and implementation so that the function does the append directly, instead of creating a short-lived temporary object. - When extracting strings from a set of input data, try to return a substring of the original data, instead of creating a copy. You will create a new
String
object, but it will share thechar[]
with the data. (The trade-off being that if you’re only using a small part of the original input, you’ll be keeping it all around in memory anyway if you go this route.)
Tip 2: Prefer Static Over Virtual
If you don’t need to access an object’s fields, make your method static. Invocations will be about 15%-20% faster. It’s also good practice, because you can tell from the method signature that calling the method can’t alter the object’s state.
Tip 3: Use Static Final For Constants
Consider the following declaration at the top of a class:
static int intVal = 42; static String strVal = "Hello, world!";
The compiler generates a class initializer method, called <clinit>
, that is executed when the class is first used. The method stores the value 42 into intVal
, and extracts a reference from the class file string constant table for strVal
. When these values are referenced later on, they are accessed with field lookups.
We can improve matters with the “final” keyword:
static final int intVal = 42; static final String strVal = "Hello, world!";
The class no longer requires a <clinit>
method, because the constants go into static field initializers in the dex file. Code that refers to intVal
will use the integer value 42 directly, and accesses to strVal
will use a relatively inexpensive “string constant” instruction instead of a field lookup.
Note: This optimization applies only to primitive types and String
constants, not arbitrary reference types. Still, it’s good practice to declare constants static final
whenever possible.
Tip 4: Avoid Internal Getters/Setters
In native languages like C++ it’s common practice to use getters (i = getCount()
) instead of accessing the field directly (i = mCount
). This is an excellent habit for C++ and is often practiced in other object oriented languages like C# and Java, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.
However, this is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It’s reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.
Without a JIT (Just In Time compiler), direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter.
Note that if you’re using ProGuard, you can have the best of both worlds because ProGuard can inline accessors for you.
Tip 5: Use Enhanced For Loop Syntax
The enhanced for
loop (also sometimes known as “for-each” loop) can be used for collections that implement the Iterable
interface and for arrays. With collections, an iterator is allocated to make interface calls to hasNext()
and next()
. With an ArrayList
, a hand-written counted loop is about 3x faster (with or without JIT), but for other collections the enhanced for loop syntax will be exactly equivalent to explicit iterator usage.
There are several alternatives for iterating through an array:
static class Foo { int mSplat; } Foo[] mArray = ... public void zero() { int sum = 0; for (int i = 0; i < mArray.length; ++i) { sum += mArray[i].mSplat; } } public void one() { int sum = 0; Foo[] localArray = mArray; int len = localArray.length; for (int i = 0; i < len; ++i) { sum += localArray[i].mSplat; } } public void two() { int sum = 0; for (Foo a : mArray) { sum += a.mSplat; } }
zero()
is slowest, because the JIT can’t yet optimize away the cost of getting the array length once for every iteration through the loop.
one()
is faster. It pulls everything out into local variables, avoiding the lookups. Only the array length offers a performance benefit.
two()
is fastest for devices without a JIT, and indistinguishable from one() for devices with a JIT. It uses the enhanced for loop syntax introduced in version 1.5 of the Java programming language.
So, you should use the enhanced for
loop by default, but consider a hand-written counted loop for performance-critical ArrayList
iteration.
Tip 6: Consider Package Instead of Private Access with Private Inner Classes
Consider the following class definition:
public class Foo { private class Inner { void stuff() { Foo.this.doStuff(Foo.this.mValue); } } private int mValue; public void run() { Inner in = new Inner(); mValue = 27; in.stuff(); } private void doStuff(int value) { System.out.println("Value is " + value); } }
What’s important here is that we define a private inner class (Foo$Inner
) that directly accesses a private method and a private instance field in the outer class. This is legal, and the code prints “Value is 27” as expected.
The problem is that the VM considers direct access to Foo
‘s private members from Foo$Inner
to be illegal because Foo
and Foo$Inner
are different classes, even though the Java language allows an inner class to access an outer class’ private members. To bridge the gap, the compiler generates a couple of synthetic methods:
/*package*/ static int Foo.access$100(Foo foo) { return foo.mValue; } /*package*/ static void Foo.access$200(Foo foo, int value) { foo.doStuff(value); }
The inner class code calls these static methods whenever it needs to access the mValue
field or invoke the doStuff()
method in the outer class. What this means is that the code above really boils down to a case where you’re accessing member fields through accessor methods. Earlier we talked about how accessors are slower than direct field accesses, so this is an example of a certain language idiom resulting in an “invisible” performance hit.
If you’re using code like this in a performance hotspot, you can avoid the overhead by declaring fields and methods accessed by inner classes to have package access, rather than private access. Unfortunately this means the fields can be accessed directly by other classes in the same package, so you shouldn’t use this in public API.
Tip 7: Avoid Using Floating-Point
As a rule of thumb, floating-point is about 2x slower than integer on Android-powered devices.
In speed terms, there’s no difference between float
and double
on the more modern hardware. Space-wise, double
is 2x larger. As with desktop machines, assuming space isn’t an issue, you should prefer double
to float
.
Also, even for integers, some processors have hardware multiply but lack hardware divide. In such cases, integer division and modulus operations are performed in software—something to think about if you’re designing a hash table or doing lots of math.
Ok, that’s all. Thanks for your reading.
Please press “Like” button, please please please! 😀 😀 😀