Why does Android App Lag ?

Why does Android App Lag ? 


This is a common problem in Android devices if we are not careful enough with our design the android app starts to lag.

Let's understand first how lag happens,

Redmi Note 7 Pro(60Fps)
Suppose you you have brought a device for which frame rate is 60fps . That means your android screen is going to be updated every
1000ms / 60 ~ 16ms 

Android app uses the main thread to update the ui every 16ms. 

Now if the main thread is blocked for example downloading some image or filtering some data then for more than 16ms then android app will not be able to render the ui and it will skip the frames and wait for the next cycle. It will show us a lag.


Here are some primary reasons why main thread might be blocked, - 

  • Running GC(Garbage Collector) too frequently.
  • Memory Leaking
  • IO operation on the Main thread
  • Long running calculations on the main thread
  • Loading Bitmaps everytime api  gets called.

Here are some solutions to avoid lag, -

  • Use StrictMode
  • Use threads and coroutines for operations 
  • Don't leak contexts in inner classes.
  • Use static inner classes over non-static.

Comments