This article is all about creating a better Android app (no more lags!) and building a smooth-running interface. We will discover how to increase the performance of an Android application. First and foremost, we must determine one big question:
Why does the Android App slow? or Why does an Android app lag?
We will begin with the themed culprit behind your app performing low:
Garbage Collector: The Android Evil Tax!
The major cause of the Android App’s bad performance is that it executes GC often. In a nutshell: During the time when GC is executing, the real app is not running. So, when the Android App runs, it allocates many objects based on your code, and when those objects are no longer referred to, the system calls GC when there is memory pressure to deallocate those objects, so if the object allocation and deallocation occur on a regular basis, the GC runs on a regular basis to release memory, so the more time the GC runs, the less time your app runs. As a result, it appears like the app is sluggish. As a result, the application user has a negative experience.
How to Ensure Smooth UI and Transitions?
For smooth UI rendering, the Android App refreshes its UI every 16ms (assuming 60FPS -> 1000ms/60 = 16.67ms16ms). So, if the GC is operating during that period, the app is unable to refresh the UI, resulting in a few frames being skipped, giving the impression that the app is sluggish. So the real cause is that the GC was running or that the work was being done in the main thread too quickly, preventing the app from rendering its UI smoothly.
Another cause is that the app may be performing too much in the main thread, and if any method/task takes longer than 16 – 18ms, the app will be unable to refresh the UI, resulting in a lag in the app for that period.
GeekTip: Essentially, the system attempts to refresh the UI every 16 – 18 ms.
What if our main thread work takes longer than 16ms? For instance, suppose our work takes 26ms. The system tries to refresh the user interface, but it is not ready. As a result, nothing will be refreshed. As a result, the UI was updated after 32ms rather than 16ms. There is a missing frame.
There will be no smooth motion even if one frame is lost. It will be sluggish for the user. These are the causes behind the Android App’s poor performance.
How to Improve?
To maximize it, we must concentrate on the following:
- Reduce the time it takes for the GC to run
- In the main thread, don’t do much.
- Use alternative threads, and threads in Android.
Steps to Increase Performance Include
- Make use of ArrayMap and SparseArray. This post will explain why and when you should utilize ArrayMap and SparseArray to improve your Android applications.
- Do not allocate an object too early; instead, allocate an object only when it is needed. Make use of lazy initialization.
- If an object is not necessary, it should not be allocated.
- Heavy work should be kept away from the main thread. Put it in the background thread.
- To avoid memory churn, employ the idea of object pools. Learn more about the bitmap pool by clicking here.
- Avoid Using Auto-Boxing as Integer, Boolean, and so on consume more memory as classes such as Integer consume more memory, therefore use int wherever feasible instead of Integer.
- For constants, use static final
- When possible, avoid using internal getters/setters (direct field access is 3x faster)
- Contexts should not be leaked in inner classes.
- Use static inner classes over non-static inner classes.
- To minimize duplicate bitmap decoding, use LRU cache for the bitmap; this lowers GC calls repeatedly.
- In Android Development, use StrictMode to find things you performed by accident, such as an unintentional disc or network access or database query on the application’s main thread.
- Use GPU Rendering Profiles: It provides an instant visual depiction of how long it takes to draw the frames of a UI window in comparison to the 16ms benchmark.
Finally, avoid allocating an excessive number of objects.