Android App performance tips for developers

Introduction
Performance is the key factor for any app. It mainly depends on the algorithm and data structure's you choose. Some improvements listed below can micro-optimize and improve overall app performance of the app. The better the app responds fast ,the more downloads and users you have.
Before you start optimizing the code, check the Profilers and identify where the app is taking much time or responds slowly,measure existing performance and compare. Otherwise it is of no use of optimizing the code. Though these may look simple, but following these practices makes a huge difference.
check the link below to know how to use Android Profiler-
Table of Contents
- Don’t create unnecessary Objects
- Avoid using floating-point
- Don’t operate long operations on the Main UI Thread
- Use enhanced for loop syntax
- Avoid using nested Layouts
- Know and use the libraries
- Remove unused Resources and code
- General tips
- Wrapping up
- References and Links
Don’t create unnecessary Objects
Try to avoid creating short term temporary objects if you can.Lesser the objects ,means less- frequent garbage collection.
Example- String Vs StringBuilder
if you want to append more strings,then we would write the code in this way,

This creates tons of Garbage Collections(GC) .we can write the above code in the following way-

which creates less Garbage collection(GC) and improves performance as well as memory,this is useful while appending large amount of strings.
Avoid using floating-point
Integer is 2x faster than floating-point on Andriod devices.so try to avoid wherever it requires.
Don’t operate long operations on the Main UI Thread
Blocking the main UI Thread causes serious performance issues, Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user’s perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous “application not responding” (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.
Async Task is ideal for working on and off UI Thread.
Handler Thread is useful when we need a dedicated thread for API callbacks.
Thread Pool is useful for breaking our work into small packets and push those to another threads.
Intent Service is ideal for background tasks or when you needed to get Intent work off the UI Thread.
Use enhanced for loop syntax
For example consider below code alternatives for iterating through an array:

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.
Avoid using nested Layouts
Try to avoid using Layout inside Layout,because whenever we use any Layout, android draws the view,which takes 2 passes to calculate the size of that view(height and width). Using nested layouts , can lead to deep view hierarchy, if we use nested layouts which use layout_weight parameter can be expensive as child needs to be measured twice.
Know and use the libraries
Always know the API(s) and the libraries that you use. Always prefer to use a library’s code over writing your own. Remember that the system has the privilege to replace your calls to the library method(s) using a hand-coded assembler, which could be even better than the best code the JIT can come up with for the equivalent Java.
A classic example here would be the String’s IndexOf method and associated APIs, which Dalvik will replace with an inlined intrinsic. Similarly, the System’s ArrayCopy method is about 9x faster than a hand-coded loop on a Nexus One with the JIT.
When you decide to use an external library, you may need to optimize that library for mobile devices. Plan for that work up-front and analyze the library in terms of code size and RAM footprint before deciding to use it at all.
Remove unused Resources and code
Remove unused resources which are of no use to the app likes strings,variables,drawables etc. In Android Studio Menu > Refactor > Remove Unused Resources… Select the resources you want to remove. Remove unused code which is not required in the app.
General Tips
I think these tips, you have already know about these or implemented in your code , I just review those
- use Parcelable Over Serializable
- Use Cache Memory
- Use Recycler View instead of List View
- Use Static Final For Constants
Wrapping up
Everyday is a new day, by reading my blog, you have learnt something new today and I think it is resourceful and knowledgeable. If you find anything wrong in my blog, please comment it so that I can fix it up.Please comment down below, if you have improved your app performance.
Smash that clap button if you liked this post.
References and Links
Jason D,2017,’Fast Loading Speed’[image],viewed 3 November 2019, <https://thimpress.com/ways-wordpress-meet-user-expectations/fast-loading-speed/>.
