1

I'm getting an InflateException preceded by an OutOfMemoryError exception thrown. I understand that I should be able to solve the issue by either reducing the size/resolution of the image, or by adding android:largeHeap="true" to my manifest, but something peculiar is happening.

This exception is not thrown when I first inflate the view. I have an Activity that I'm using which contains a PreferenceFragment. When you select a row in the PreferenceFragment you are brought to a Fragment which shows some images and text. The exception is only thrown after navigating back and forth between several of these fragments. It seems to me that something is not being properly disposed of since this will occur on any one of the fragments, but never occurs the first, second, or third time.

Is there a way I can ensure that everything is being disposed of when going back from one of these fragments?

Line where exception is thrown in OnCreateView of Fragment:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.Inflate(MyResource, container, false);
}

Also worth noting, exception is only thrown after several different fragments are opened from the activity. You could go back and forth between the same one all day and the exception would not be thrown.

Jared Price
  • 5,217
  • 7
  • 44
  • 74
  • 1
    GC.Collect will 'work', but I would look at http://stackoverflow.com/a/28868582/4984832 in order to actually understand the issue and how to Dispose / OnDestroy so your fragment(s) are cleaned up properly – SushiHangover Dec 30 '15 at 17:51

2 Answers2

3

I came across something similar while developing one of my apps. In my app, clicking a list item, showed the user an image in a separate fragment. After clicking three or four list items. I saw an out of memory exception, similar to the one you describe.

I solved this by explicitly calling the garbage collection in my image display fragment. C# code for Xamarin as follows:

System.GC.Collect ();

JonLord
  • 807
  • 10
  • 24
  • 1
    That´s correct. Here´s the reason why (in the accepted answer): http://stackoverflow.com/questions/34474379/shouldn%C2%B4t-gc-run-automatically-in-xamarin-android-before-runing-out-of-memory – xleon Dec 31 '15 at 07:18
0

I ported this from Android to C#

https://github.com/koush/UrlImageViewHelper

To deal with large images, I can post the code if you need it.

Eric
  • 462
  • 1
  • 5
  • 13