Why does an Android App lag?
Tags: android
Published: 2022-09-19 03:36:53

---
title: Why does an Android App lag?
published: true
description: In this blog, we will learn why an Android App lags. I believe that every Android Developer should know the reason for the low performance of the Android app.
tags: android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ixh8vs3bh8twulxa71j.png
canonical_url: https://amitshekhar.me/blog/android-app-lag
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

In this blog, we will learn why an Android App lags. I believe that every Android Developer should know the reason for the low performance of the Android app.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/android-app-lag).**

I will start with the following statement:

> **Garbage Collector: The TAX on Android App Performance**

The main reason for the Android App lag or for the low performance of the 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.**

Basically, when the Android App runs, it allocates many objects on the basis of your code, and when the 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 the regular basis, it means that the GC will run on a regular basis to release memory.

Here, the important point is that the time for which the GC runs, your app does not run for that time. So, it seems that the app is lagging. And hence, a bad experience for the user of the application. Let's understand it deeply.

The Android app updates its UI every `16ms` (considering `60FPS -> 1000ms/60 = 16.67ms ~ 16ms`) for smooth UI rendering.

Frame per second (FPS) is the measurement of the number of frames that appear within a second.

![Android frame update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhumylka0zhwrcnpcvjr.png)

So, if the GC runs for more time, the app will be unable to update UI and it will skip a few frames, so it will seem that the app is lagging. That is one of the reasons for the app lag.

Another reason is that the app is doing too much on the main thread. For example, 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 every `16ms`, and 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. In that case, it will not refresh anything. And, that caused the UI to be refreshed after `32ms` instead of `16ms`. Ultimately, there will be a **frame drop**.

![Android frame update delayed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zj7eoh6zkuk6z5ixxg6s.png)

So, due to the frame drop, the user finds the app laggy.

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

Now, we have understood the two most important reasons because of why the Android Apps lag which are as follows:

- **GC running frequently**
- **Doing too much work on the main thread**

In order to fix this lag issue, we need to focus on the following:

- Reduce GC running time
- Do not do too much on the main thread

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
- [Facebook](https://www.facebook.com/amit.shekhar.iitbhu)

Learn about OkHttp Interceptor: [OkHttp Interceptor](https://amitshekhar.me/blog/okhttp-interceptor)