Android ViewPager with bitmaps causing memory leak

SmeSme USMember ✭✭✭
edited September 2017 in Xamarin.Forms

I implemented an activity in my Android project, to allow the user to flip through images by swiping left and right. I would have used Forms' CarouselPage, but I wanted the ability to pan/zoom the image, and couldn't do so with CarouselPage.

The problem is, the heap size is increasing every time I flip to a new image, but when I exit the activity, the heap size never decreases, which is obviously pointing to the problem being the bitmaps are not getting garbage collected.

For the ViewPager, I am using PagerAdapter to load/destroy the views. Even if I manually call bitmap.Dispose(), bitmap.Recycle(), GC.Collect(), Java.Lang.JavaSystem.Gc(), (I tried everything), the heap size never decreases. Eventually the heap gets to 192MB and causes an out-of-memory exception.

Does anyone have any experience with this issue? Is it related to Mono at all?

Posts

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @Sme,

    Personally not a fan of ViewPager, rather I prefer to stick to XF controls, However some same sort of things went in before.

    Hope the following SO post is helping you.

    -- N Baua

  • SmeSme USMember ✭✭✭
    edited September 2017

    @N_Baua said:
    Hi @Sme,

    Personally not a fan of ViewPager, rather I prefer to stick to XF controls, However some same sort of things went in before.

    Hope the following SO post is helping you.

    -- N Baua

    I too would prefer to keep my code simpler and use XF controls, but even after trying to use a Custom CarouselPage renderer, XF just doesn't provide the functionality I needed to be able to pan the image (or moreso, preventing the 'swipe left/right' gesture on the Carousel page from firing when panning the image)

    The code I have is similar to the code in the SO post. In the destroyItem() method, I tried disposing/recycling the bitmap, cleaning up the ImageView, but the bitmap remains in memory no matter what I try.

Sign In or Register to comment.