Performance Tips - Smooth Running Android App

Banner Smooth Running Android App Tips

This post is all about making a better Android App (No lags now! only smooth running UI). We will learn what can we do to improve the performance of an Android application.

First of all, we should learn why the Android App lags?

Why Android App lags?

I will start with the following statement:

Garbage Collector: The TAX on Android App Performance

The main reason for the low-performance Android App is that it runs GC very frequently.

Simple in one line: The time for which GC is running the actual app is not running.

So, when the Android App runs, it allocates many objects on the basis of your code, and when the objects that are no longer referred, the system call GC when there is memory pressure to deallocate those objects, so if the object allocation and deallocation are occurring on regular basis, the GC is running on a regular basis to release memory, So more time the GC is running, your app is not running for that time. So it seems the app is lagging. And hence, a bad experience for the user of the application.

The Android App updates its UI every 16ms (considering 60FPS -> 1000ms/60 = 16.67ms~16ms) for smooth UI rendering . So if the GC is running for that time, the app is unable to update UI for that time, which leads to skipping of few frames, so it seems that the app is lagging. So the actual reason is that the GC was running or may the work is doing too much in the main thread so that the app did not get time to render it’s UI smoothly.

Another reason is that maybe the app is doing too much in the main thread, so at that time if any method/task is taking more time than 16ms the app will be unable to update UI, which means lag will be there in the app for that time.

Basically, the system tries to redraw the UI after every 16ms.

Android Frame Update

What if our task on the main thread takes more than 16ms. For example, if our task takes 26ms. The system tries to update the UI, but it wasn’t ready. So it will not refresh anything. And, this caused the UI to be refreshed after 32ms instead of 16ms. There is a dropped frame.

Even if there is one dropped frame, there will no smooth animation. The user will find it laggy.

So, these are the reasons for the low-performance of the Android App.

How to optimize it?

In order to optimize it, we need to focus on the following:

  • Reduce GC running time
  • Do not do much in the main thread

Steps to be taken to improve the performance:

  • Do not allocate any object if it is not required.
  • Do not allocate object early, allocate object only when it is required. Use lazy initialization.
  • Avoid Auto-Boxing as Integer, Boolean, etc takes more memory as classes like Integer takes more memory so use int where ever possible instead of Integer.
  • Use ArrayMap and SparseArray. Refer to this article, this will show why and when to use ArrayMap and SparseArray to optimize your Android Applications.
  • Use the concept of object pools to avoid memory churn. Learn about the bitmap pool from here.
  • Keep heavy work away from the main thread. Transfer it on the background thread.
  • Use static final for constants (or const val in Kotlin).
  • Avoid Internal Getters/Setters wherever not necessary (direct field access is 3x faster)
  • Don’t leak contexts in inner classes.
  • Use static inner classes over non-static.
  • Use LRU cache for the bitmap to avoid redundant decoding of bitmap, it reduces GC calling again and again.
  • Use StrictMode to find things you did by mistakes like an accidental disk or network access or database query on the application’s main thread in Android Development.
  • Use Profile GPU Rendering: It gives a quick visual representation of how much time it takes to render the frames of a UI window relative to the 16ms benchmark. You can enable it from Settings-> Developer Options-> Monitoring Section -> Select Profile GPU Rendering.
  • And finally, do not allocate a large number of unnecessary objects.

This is how we can improve the performance of an Android Application.

That's it for now.

Happy Learning :)

Show your love by sharing this blog with your fellow developers.

Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.