0

I have some memory leak problem I can't find the way to solve. There are many questions in stackoverflow but no one of them was useful for me.

I have an activitiy with a view pager and action bar tabs navigation. In every fragment I load only one image so that this is sort of a gallery in the end. That works nice.

After some fragment changes, I got this error:

E/AndroidRuntime(19271): java.lang.OutOfMemoryError E/AndroidRuntime(19271): at android.graphics.Bitmap.nativeCreate(Native Method)

All I've read is about the need of deallocating images, so I thought something about:

  1. Event at "fragment lost focus"
  2. Delete all views inside the fragment whose focus has lost

But I can't find any solution like that. Is that the correct way? If so, can you give an example? Or I'm completely wrong and the solution is another way?

This is how I add the images to the fragment:

             ImageView img = new ImageView(rootView.getContext());
             img.setVisibility(1);
             img.setImageResource(R.drawable.gallery_image_1);
             img.setAdjustViewBounds(true);
             img.setTag( "gallery_image_1" );
             gallery.addView(img);

Thanks to all in advance!

Diego

1 Answer 1

0

First of all, make sure you're using FragmentStatePagerAdapter, as it's more memory-friendly than FragmentPagerAdapter.

Second, analyze your application with MAT to find what really causes memory leaks. Here's an excellent post describing how this tool can be used in context of Android.

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

2 Comments

Thanks for your answer. I changed to FragmentStatePagerAdapter but it hasn't helped ( I'd say it works worse now ). I installed MAT but apart from telling me that byte[] ( which is where bitmap info is stored ) is the class which is filling the memory, I can't move forward to a solution.
In the end, it seems I found a simple solution here: stackoverflow.com/questions/18583485/… It works perfectly by now to me.

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.