Android ViewPager memory leak

I need to create a ViewPager in Android with 5 slides, each of which consists of an image and text. I have an array with resources for images:

private static final int[] images = {R.drawable.tutorial_step_01, R.drawable.tutorial_step_02, R.drawable.tutorial_step_03, R.drawable.tutorial_step_04, R.drawable.tutorial_step_05, R.drawable.tutorial_step_06}; 

then I create an adapter:

 @Override public Object instantiateItem(ViewGroup container, int position) { LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.tut_slide, null); TextView title = (TextView) tv.findViewById(R.id.tut_title); title.setText(getResources().getText(titles[position])); TextView content = (TextView) tv.findViewById(R.id.tut_content); ImageView image = (ImageView) tv.findViewById(R.id.tut_image); slide_image = BitmapFactory.decodeResource(getResources(), images[position]); image.setImageBitmap(slide_image); content.setText(getResources().getText(contents[position])); ((ViewPager) container).addView(tv, 0); return tv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((LinearLayout) object); 

//
}

the problem is that the android does not want to collect the image after selecting another page. Thus, after 10-15 changes, it leaves the OutOfMemory exception. Then I added to the string initialization

 if (slide_image!= null) { slide_image.recycle(); System.gc(); } 

And it works well! But except for one: I have a black screen instead of the first image, which is replaced with a real one after several coups. So I don’t know what to do with memory leak

+4
source share
3 answers

Well, I finally solved the problem. I came across this very similar case, and since I saw so many questions related to the same problem, I chose this question because it has not yet been answered. PagerAdapter must call the destroyItem method not only when it is superior to offLimitScreenPageLimit , but also when the screen is rotated, but it is not, therefore, it must be forced to do so ... to achieve it, you just need to set the adapter to zero in the onStop or onDestroy activity.

 @Override protected void onDestroy(){ pager.setAdapter(null); } 

Hooray!

+4
source

It is not clear what you are using, but I ran into a similar problem.

I assume that you are using the FragmentPagerAdapter.

When scrolling with this adapter, it does not destroy pages out of sight and from the cache. If there is an ImageView in the fragment used by the FragmentPageAdapter, OOM is inevitable

Just change the adapter extension to

FragmentStatePagerAdapter

This will destroy fragments that are not in use, and leave more memory for new fragments.

This is still not perfect, I found that sometimes I can scroll faster than the garbage collector collects destroyed bitmaps, but its pretty damn close.

If I were looking to improve it, I would override destroyItem and then get the bitmap used in the image view and the .recycle bitmap.

Bitmap Recycle ImageView

+1
source

This behavior should not be related to a memory leak. This seems to be related to when and how you process bitmaps in your life cycle to update your view. Try calling onPageSelected () or notifyDatasetChanged () manually at some point in your initialization.

This solution may not solve the problem completely, but try. It's hard to say with your explanation.

0
source

All Articles