How to solve frequent garbage collection in android?

1.4k Views Asked by At

Our team is stuck in the problem, our android application lags after using some time, and here is the Android Profiler for the memory.

Here is the image for Android Profiler

I am noticing that the garbage collection is too frequent and I resolved almost every memory leak for our application.

Points to remember:

  • Is this caused due to any module or library??
  • Using too many images in nested recycler views like Playstore.
  • Using AutoScrollViewPager with 1.5 secs of delay
  • Using image library Glide for loading image in whole application.

This is happening after some seconds in verbose logs:

2022-03-15 16:37:47.761 30333-30358/com.app I/com.app: Background concurrent copying GC freed 417454(8609KB) AllocSpace objects, 2(328KB) LOS objects, 27% free, 16MB/22MB, paused 241us total 153.080ms

2022-03-15 16:37:51.226 30333-30358/com.app I/com.app: Background young concurrent copying GC freed 330919(6815KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 14MB/20MB, paused 685us total 129.321ms

2022-03-15 16:37:51.580 30333-30358/com.app I/com.app: Background concurrent copying GC freed 520664(10MB) AllocSpace objects, 1(132KB) LOS objects, 31% free, 12MB/18MB, paused 131us total 165.951ms

2022-03-15 16:37:58.195 30333-30358/com.app I/com.app: Background concurrent copying GC freed 391436(8248KB) AllocSpace objects, 1(68KB) LOS objects, 26% free, 16MB/22MB, paused 426us total 239.756ms

Any help will be appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

After doing some R&D and doing work on suggestions, Our app is 50-70% (Depending on RAM on different devices) performing better as per before.

I raised some points for the issue, I configure that:

  • Is this caused due to any module or library??

Some modules make our APK size a little bit large at the time of debugging. So, if we apply progaurd or use bundle then our app APK will become less in size and will also take less RAM at that time.

  • Using too many images in nested recycler views like Playstore.

Use the famous libraries for image loadings like glide or Picasso, we need bitmap pooling for images which we are using again and again. This will help us to avoid more and more bitmaps for the same image URLs

Reference: Here is the good guidance, how to optimize recycler view

  • Using AutoScrollViewPager with 1.5 secs of delay

Here is the major issue that comes, after using the app for 3-5 mins, GC calls so frequently, and that is due to ViewPager.setCurrentItem(). For the whole application where we are using ViewPager and changing pages GC calls.

In AutoScrollViewPager there is a delay with 1.5 secs and then ViewPager AutoScrolls, then GC calls. That's why there are too many GCs in the android profiler after every 2 secs.

So, we removed the code for AutoScrolling, performance is better from the last scenario.

Some other optimizations are:

  • Replaces multiple instances to singleton instances.
  • Removed all Leaks from the application.

Reference: Here is about Memory Leaks in Android

Any suggestions for this issue will be helpful for all. Thank you