App performance is the key to success of your app, and everyone knows it, but the question is - how can you achieve better performance. Today we learn to improve app performance by doing right things. This should be our main priority to choose right algorithms and data structures. You always use these things for code efficiency and code optimization in every day coding. Therefore you should follow bellow two things:
- Write only those codes which your app needs.
- If you can avoid memory allocation, then go for it.
1.Never create unwanted objects
If you create object, it will cost something. Garbage collector always does his job. But it will cost something to free memory. But if you are not creating any unwanted objects, then garbage collector don't have any extra job to do. And it will be better for your app. Allocating memory is more costly than not allocating memory.
So please always avoid unnecessary work. 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.
2.Prefer Static Over Virtual
Make your function static, if you don’t need to access fields of object. This change makes your function calling 15-20% faster. It's also good habit
3.Use Static Final for Constants
You can improve performance by using the final keyword. Variables that declare as static final can be access directly from the static field initializers of the dex file. The access to static final is relatively inexpensive instead of a field lookup.
4.Avoid Using Floating-Point
The rule of thumb says that, on the Android devices, floating-point is about 2X slower than integer-point
If you are considering speed, doubles and floats are relatively same on the latest hardware but by the term of space, double is 2x larger than float.
5.Use of Thread
Use of thread on the UI is also good practice. This individually would solve the problem of smooth scrolling. But never do the following things on UI thread.
- JSON Parsing.
- Networking call.
- JSON parsing.
6. Power consumption
Power consumption is also major part of Android app development. Always focus on how you can reduce battery usage. Following tips are useful to improve battery usage.
- Avoid Network calls as much as you can.
- Carefully handle GPS.
- Carefully handle Alarm Manager.
7. Use AsyncTask Call
The use of AsyncTask is also good programming habit. The use of AsyncTask enables easy and proper use of UI thread. AsyncTask runs on background thread.
Though AsyncTask looks easy and advantageous, It has some disadvantages. If the Activity that creates AsyncTask has been destroy, you will not expect that AsyncTask also be destroy. The AsyncTask keeps running in background. There is also Memory leak problem in the AsyncTask.
Above were some of the methods I follow to boost the performance. Hope you would find it useful. If you have some more ways do comment them.