0

I have a RecyclerView which is showing a CardView. In the CardView there are two items:

  1. TextView
  2. My custom view with underlying bitmap

The bitmap is created dynamical.

After some up and down scrolling I get an OutOfMemoryException.

I'm not sure how to handle it? Should I use LRUCache? Or third party libraries like Picasso - which seems to only work on urls and ids?

Any help appreciated

Update:

public class ManageProfileAdapter : RecyclerView.Adapter
{
  public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
  {
    ManageProfileViewHolder vh = holder as ManageProfileViewHolder;

    vh.Caption.Text = profiles[position].Name;
    vh.Thumbnail.SetProfile(profiles[position].Profile);
  }
}

The thumbnail class is a custom class derived from View (which includes bitmap):

public class ThumbnailView : View
{
   private Canvas DrawCanvas;
   private Bitmap CanvasBitmap;
}

The bitmap is drawn on the Canvas.

  • Could you please minimum provide us your code of the ViewAdapter for the RecyclerView? Possible it is your handling off the cardview in that adapter, holding to many bitmaps instead of relasing the memory again, when out of view. – Rene M. Apr 12 '16 at 11:28
  • Picasso also works with local images, both from assets dir, or local device storage – bvanvelsen - ACA Group Apr 12 '16 at 11:30
  • Also see this topic: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object?rq=1 – bvanvelsen - ACA Group Apr 12 '16 at 11:31

2 Answers2

1

You must be creating bitmap data somewhere in your code again and again. Check it out

resw67
  • 139
  • 3
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/11988188) – Laur Ivan Apr 12 '16 at 15:07
  • Sorry for that but he didn't provide any code back than and I tought he asked for opinions of others. – resw67 Apr 12 '16 at 15:55
  • No worries. Can you please update your answer based on the new information? – Laur Ivan Apr 13 '16 at 08:13
0

This guided me in the right direction:

You must be creating bitmap data somewhere in your code again and again. Check it out

The bitmap in my thumnail view is not removed by the garbage collector.

The solution is:

  1. Override the OnDestroyView event of your Fragment
  2. Call adapter.Dispose() inside it
  3. In your adapter add a List of your ViewHolderItems
  4. In the OnCreateViewHolder method add the ViewHolderItems to your List
  5. Override the Dispose method of your adapter
  6. In the Dispose method go through your list and dispose the bitmap of the ViewHolder
  7. Finally clear your ViewHolder list

I got no memory exceptions anymore and in logcat my highest usage is 30mb ram.

Lesson Learned: You have to dispose bitmaps yourself in RecyclerView

Thank you very much.