Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – RecyclerView inside RecyclerView, app crashing due to high memory consumption

Posted on November 14, 2022

Solution 1 :

Trust me, dont go with recyclerview inside recyclerview.
I had the similar situation in my current app where my colleague built solution by using nested recyclerview.

I rewrote the entire logic using insert and delete with animation and multiple view type.
It will involve some extra code to manage it. But the result would be quite satisfying.

In fact, I used same logic in iOS collectionview as well. App on both platform is live.

Solution 2 :

Try add this to your manifest.xml

        android_largeHeap="true"

Solution 3 :

Your memory consumption is likely to high because you retrieved all the photos from the album at once with the line:

ArrayList<PhonePhoto> phonePhotos = phoneAlbums.get(position).getAlbumPhotos();

I assume you are also pulling all the photos at their original quality, so having 200+ full size photos on phone’s RAM all at once could be the cause of the crash.

One way you could fix this would be to load in lower resolution previews of the photos into RAM, and once a photo is actually being displayed in the RecyclerView you could load a full resolution photo.

Problem :

I wanted to create a recycler view of photo albums and when the user taps on an album, it expands to grid layout showing images in that album.

For example, non-expanded:

enter image description here

and expanded:

enter image description here

The layout of outer recyclerView item only contains a textView, checkbox, and an invisible recyclerview that becomes visible on click of the item.

I declared the outerAdapter like this:

photosVideosAdapter = new PhotosVideosAdapter(getContext(),new ArrayList<PhoneAlbum>());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(photosVideosAdapter);

Here is the onBindViewHolder of outer adapter:

holder.tvFolder.setText(phoneAlbums.get(position).getName());
ArrayList<PhonePhoto> phonePhotos = phoneAlbums.get(position).getAlbumPhotos();
InnerAdapter innerAdapter = new InnerAdapter(context, phonePhotos);
holder.innerRecyclerView.setHasFixedSize(true);
holder.innerRecyclerView.setLayoutManager(new GridLayoutManager(context,3));
holder.innerRecyclerView.setAdapter(innerAdapter);
.... OnClickMethod to toggle visibility of innerRecyclerView

And onBindViewHolder for inner adapter:

Glide.with(context)
        .load(phonePhotos.get(position).getPhotoUri())
        .override(200,200)
        .into(holder.imgThumbnail);

The problem is that if inner recyclerView contains more than 200 items, the app crashes due to high memory usage. But as you can see, I’m using glide to load images and also RecyclerView shouldn’t create all the views at once. But, what I can see is that the inner RecyclerView is creating all the item views at once which is causing the app to crash.

READ  [ANSWERED] (Java - Android) How do I smoothly unpack songs and play them on my Android app?
Powered by Inline Related Posts

How can I fix this problem? Any help will be appreciated.

Comments

Comment posted by Jazib Khan

The problem is that the inner recyclerView is loading all the views at once. So, if the inner recyclerView has 1000 images, all images are loaded at once, which is not what recyclerView is supposed to do.

Comment posted by Jazib Khan

The problem is not about increasing the heap size. The problem is that the inner recycler view is creating all the view at once, so, if there are 4000 photos in an album, it will create all the 4000 view at once.

Comment posted by Anirudh

One way you could test if this is actually the root cause of your problem would be to comment out all the code regarding the displaying the pictures in a RecylcerView and seeing if the memory usage is still very high.

Comment posted by Jazib Khan

PhonePhoto only contains the URI of the images, and all the image’s URI are already stored in ArrayList.

Comment posted by Jazib Khan

The problem is that the inner recycler view is loading all the views at once. So, if inner recycler view has 1000 images, all images are loaded at once, which is not what recyclerView is supposed to do.

Comment posted by Anirudh

Have you tried logging what layoutManager.getChildCount(); returns, that should be the number of views that are currently loaded

Comment posted by Jazib Khan

For innerRecyclerView, it is equal to the number of images.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress