0

I am using itemTouchHelper to remove a cardView from a RecyclerView on swipe my cardview is simple it just has a text and an image but when i swipe to remove the card disappears but is never taken out of memory (i am saying this because the memory of the image never goes away and it causes an out of memory exception) my code is very simple so i was wondering if there was a step i was messing that tells the recycler view to remove from memory? my code is exactly like this one

1 Answer 1

1

Usually, images are loaded in android memory, scaled according to device screen resolution requirements, and kept there till the process dies or, is killed or, is collected by garbage collector.

It is possible that your image size in card view (or other images in your res folder) is/are large, and images after scaling by android is occupying too much of memory, to cause memory exception. Try reducing size of your image (Try compare size with the icon sizes that google icons has, they all are less than a Kb, amazing !).

If the above does not work, then create a drawable-nodpi folder in your res folder and put large size images in this folder, this will indicate android to not to scale these images and render it as it is. (make sure you test in all screen devices after following this approach, because now the image won't scale in different screen sizes).

Finally, if none of the above helps try running : System.gc() after there is a card swipe action performed by user. This will run garbage collector and your image will be removed from memory. (This approach does not guarantee garbage collector working in all devices, so make sure that you try for above two approaches, they should work).

Hope this helps !

2
  • thank you!, i am actually using Picasso to scale down the images so that they don't take much space, but what I noticed when I looked at the monitor for the used memory is that when I remove the image from the recycled view the memory does not go down, when I add another element the memory just keeps rising (as if the removed images are never recycled), and I cannot explain that behaviour in any other way than I am messing some step I should do to remove it from memory
    – user1
    Apr 30, 2016 at 11:09
  • Picasso has memory footprint, check out here : http://stackoverflow.com/questions/31925461/picasso-out-of-memory Apr 30, 2016 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.