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.

1
  • 1
    GC.Collect will 'work', but I would look at 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 Commented Dec 30, 2015 at 17:51

2 Answers 2

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 ();

Sign up to request clarification or add additional context in comments.

1 Comment

That´s correct. Here´s the reason why (in the accepted answer): stackoverflow.com/questions/34474379/…
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.