If you want to improve the performance of your app, then you need to use the right algorithms and data structures. There are certain coding best practices that you should ideally incorporate in your app development process, which will help you optimize your Android app. When coding, you should ideally avoid the work that you can, and only allocate the memory that needs to be allocated.
It is always good to micro optimize your app, so that you can get the best performance out of it. Here are some practices that you can adopt to optimize the performance of your Android app.
No Unnecessary Objects
When you create objects, you will need to allocate memory for the object in the app. This will work out to be slightly more expensive than not allocating memory for the object. As and when you create objects, you will schedule a garbage collector for the memory allocation, which means the user experience is hindered. Android 2.3 enhanced the garbage collector, but then it is unnecessary space within the app, which will eventually affect the user experience. It is therefore better to avoid creating objects not needed within the app.
Let’s say you have a method for returning the string, and the result will be appended to stringbuffer, then you should implement a function which directly appends instead of creating the temporary object which will return the string value. This direct approach will save the space for memory allocation, and will help you optimize the app performance.
There are many such instances in Android app development, where you can avoid creating unnecessary temporary objects.
Static Final for Constants
In case, there are object fields that you don’t need to access in your Android app, convert them into static method. This will help improve the loading time by 15% to 20%. You should convert the methods that don’t affect the app in any way.
When you declare this static, your compiler will first generate the class initialize method <linit> which is executed when the class is first used. The method will first store the value 42 and then extracts a reference from the classfile string constant using the StrVal. You can access the values using the field lookups.
Only primitive types and string constants can be optimized using this method. You should use static final to declare constants.
Avoid Internal Getters and Setters
If you have used C++ in the past, you would use getters like (i= getCount()) instead of using the field directly (i=mCount). Most of the object oriented languages use this method, as the compiler can utilize inline access. But, you can avoid this when you are developing an app on Android. When it comes to Android, the virtual method calls are slightly more expensive than the instance field lookups. You can include the getters and setters in the public interface, but you should access the fields directly within each class.
Enhanced for Loop Syntax
This for-each loop syntax is generally used for collections which utilize Iterable interface as well as for arrays. The iterator is first allocated to make the interface calls using hasNext() and next() in case of collections. For arrays, a hand written counted loop is used which works 3 times faster even without JIT. Here’s an example of the for-each loop syntax
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;
}
}
Avoid Floating Points
For Android based devices, it is a general rule that floating points tends to make the devices 2x slower than integers. Speed wise, both float and double resemble each other. But, when it comes to space usage, double is 2x larger. In case of integers, a lot of devices have hardware multiply but do not possess hardware divide. That is when, integer division and modulus operations are performed within the software, which simply means more maths.
Semaphore Software offers well optimized and aesthetically designed android apps development for your business needs. Get in touch with us via info@semaphore-software.com to know more about our services.