1

Hello i am writing a app which uses many customView(parent as FrameLayout holding activity's context).

In the app there is a recyclerView it's holder is having N(significantly large number) of view's(Custom).

As the number of View's are dynamically added (i am testing for 10k+ view's).

in recycler View i am not doing anything special but using coroutine's so that rendering of 10k view will not block the Main Thread

But due to large number's of View's in each RecyclerView Holder it's Filling up the heap memory of the app very fast as i scroll and give's OOM(Out of Memory) Crash.

To solve this issue i read and found I can use weak reference's to the View.But i have not found any example or reference to this approach.So i am not sure if using WeakReference on the Android View is a good solution or not.

Code and Demo Project Reference Below.

RecyclerView Adapter :

override fun onBindViewHolder(holder: ViewPageHolder, position: Int) {
        val slideViewMultipleCanvas = SlideViewMultipleCanvas(context)
        holder.slideViewItemHolder.addView(slideViewMultipleCanvas)
        holder.positionHolder = position
        val childJob = launch {
            //this is where 10k+ view's are created
            slideViewMultipleCanvas.setNumOfObjects(numOfItemsInViewPage)
            slideViewMultipleCanvas.startJob()
        }
        map[position] = childJob
    }

CustomView :

...
...
suspend fun setNumOfObjects(numberOfObjects: Int) {
        withContext(Dispatchers.Default) {
            for (i in 0 until numberOfObjects) {
                // To solve OOM as number of view here in heap are >10k
                listOfObject.add(WeakReference(ShapeView(context)))
            }
        }
    }
...
...
private fun addViewInScope(): Boolean {
        for (shapeView in listOfObject) {
            shapeView.get()?.let {
                addView(it)
            }
        }
        return true
    }
....
....

After Using WeakReference from my testing i have not faced any issue(NO OOM's) till now but still i get Lag's whenever GC(Garbage Collector) kick's in and collect all the weak reference.

My Main concerns are

  • Is There any other way to handle creation of huge number of View without using WeakReference?
  • Is there any issue's if i use weak reference for Views?
  • how to decrease GC lag?(even using WeakReference i am not getting smooth scroll)

P.S. : I am already handling:

  • null cases if GC collect's the weak referenced View's
  • cancelling coroutine job's when view is recycled in RecyclerView.(can i release the weak Referred view also of this ViewHolder also???)
Anmol
  • 8,110
  • 9
  • 38
  • 63

1 Answers1

1

Look into RecyclerViewPools, you should be able to allocate a specific view (if the dynamically created ones are the same view) using the itemType and have the recyclerView reuse those views also

The main lag isn’t from Weakreferences, but purely the need to generate so many views so quickly, if you could keep a cached list of views to reuse (kind of like the recyclerViewPool) then that would reduce the inflating lag

Only issue if you’ve got so many views it causes OOM, is it possible to combine the views into a generic view with multiple functionality (less physical views, but more logic and complexity in each to cater for being used under multiple situations

This would allow you to reuse the same view more times, meaning less caching/inflating, you’ll just need to make sure the code is performing efficiently

Brandon
  • 1,158
  • 3
  • 12
  • 22
  • 1
    i will look into `RecyclerViewPools` but my main aim of this demo app is to check the optimisation of memory when there are huge number's of ComplexCustomView's. These View's can be a part of RecyclerView/ViewPager etc. – Anmol Nov 13 '19 at 18:02
  • Okay! If you find a solution please post as I will be interested in how this is solved :) – Brandon Nov 13 '19 at 18:05
  • 2
    https://developer.android.com/training/improving-layouts/loading-ondemand – Brandon Nov 13 '19 at 18:07
  • https://stackoverflow.com/q/19013960/7972699 this helped me out a lot – Anmol Feb 13 '20 at 07:45