Friday, September 9, 2016

Android Application Best Practice for Performance and its Myths

Best Practices for Performance -

When anybody say you .how  can you code for best performance .we should understand and keep few things in the mind such as

1)Managing App's Memory
2)Performance Tips
3)Layout Improvement
4)Battery Life Optimization
5)Use of Multi-Threading

Each one of them itself very vast topics and very well explained on the Android Developer sites but i would like to add or you can say bring attention of Android App Developer on few topics before you going to develop any app small,large does not matter.

For example, Lets say you have an edit box and whatever you type there will be send to the server over the network.i know all of you can do this.very basic post operation on Android using HTTP connection but if i say the internet connection is very slow or very frequently it breaks.So in that case how would you make sure your app will be always responsive and will give best performance even in slow or breaking network.

So, what we need to do is, get the data insert it in preference if primitive type data or insert it in local database if Object or JSON Data and from there get data or query data and send it to the server in background So that your app will responsive always and will sync data whenever it comes in network i mean to say it will work in offline and online mode .you dont have to worry about it.

Likewise you can keep several things in the mind while developing android app.There are basically only two rules :

1)Don't do work that you don't need to do
2)Don't allocate memory if you can avoid it

So basically you should avoid creating unnecessary objects when you can avoid it because it will be  an expensive task for you.As you allocate more objects in your app, you will force a periodic garbage collection, creating little "hiccups" in the user experience.Thus, you should avoid creating object instances you don't need to.

for example: 

1)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.

2)When extracting strings from a set of input data, try to return a sub-string of the original data, instead of creating a copy.

3)An array of ints is a much better than an array of Integer objects, but this also generalizes to the fact that two parallel arrays of ints are also a lot more efficient than an array of (int,int) objects. The same goes for any combination of primitive types.

4)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.

5)Avoid Internal Getters/Setters--it's common practice to use getters (i = getCount()) instead of accessing the field directly (i = mCount). is often practiced in object oriented languages .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.Without a JIT, 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.

6)Avoid using Floating points- As a rule of thumb, floating-point is about 2x slower than integer on Android-powered devices.

7)Optimizing Layout Hierarchies
8)Re-using Layouts with <include/>
9)Loading Views On Demand- Use kind of ViewStub and all.

10)Making ListView Scrolling Smooth-using worker thread and ViewHolder


So,all these small steps will make your application fast,reliable,less bug prone,optimized and performance will be very good.


No comments: