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

Diego
  • 1

1 Answers1

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.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • 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. – Diego May 30 '14 at 22:23
  • In the end, it seems I found a simple solution here: http://stackoverflow.com/questions/18583485/android-outofmemory-drawable It works perfectly by now to me. – Diego May 30 '14 at 23:30