简体   繁体   中英

Outofmemeoryerror (viewpager + imageviews)

i am showing 150+ images in viewpager, when page size crossed 70 app is crashing , all images are loading from network , and i have fallowed [link]: Strange out of memory issue while loading an image to a Bitmap object

and i am recycling it whenever page swiping reaches 4,

for 70 page app taking 200 MB of memory.

i need help from you, how to handle it

i have to show all pages with swiping...

i have also used Runtime.getRuntime().gc();

is any way to releasing memory if app memory is reaches the 50+ MB

thanks in advance

The complete solution can be found below, the important lines are those in the destroyItem method:

private class ContentPagerAdapter extends PagerAdapter {
    @Override
    public void destroyItem(View collection, int position, Object o) {
        View view = (View)o;
        ((ViewPager) collection).removeView(view);
        view = null;
    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public int getCount() {
        return ids.length;
    }

    @Override
    public Object instantiateItem(View context, int position) {
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.findViewById(R.id.item_image);
        imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), ids[position]));

        ((ViewPager) context).addView(imageView);

        return imageView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((ImageView)object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }
    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

I think this happens because you have a memory leak, double check your variables, don't use static vars for anything big, use final when possible, make all members private.

i suggest you make a commit (or save your current code) and then try to do what i asked and see if it fixes it.

a code sample would let me tell you if you have memory leaks, maybe you can post the code somewhere like on github or google code

Bottom line: you could be doing everything right but a variable still holds a reference to your images so the garbage collector can't touch them.

I know saying you have a memory leak hurts but please don't be alarmed this happens to the best of the best, because it's so easy to happen.

NOTE: No matter how big the data i load from network apps never needed more than the size of 1 file if handled correctly.

Thanks

Sheetal,

Having looked at your code can you try the following:

@Override
public void destroyItem(View collection, int position, Object o) {
    Log.d("DESTROY", "destroying view at position " + position);
    View view = (View) o;
    ((ViewPager) collection).removeView(view);
    view = null;
}

This should release the imageView for garbage collection.

Are you loading your images in the onCreateView() view method?

The framework seems to takes care of the memory management requirements when doing it this way. I had tried loading my images in my FragmentPageAdapter passing them into my Fragment constructor preloaded or as part of the Fragment instaniateItem method but these both gave me the issue that you are facing. In the end I passed the information needed to load each image into the Fragment constructor and then used these details in the onCreateView() method.

Mark

Sheetal,

I don't have my code in front of me at the minute but it should be something similar to the following:

public class MyFragment extends Fragment {
  private Resources resources;  // I load my images from different resources installed on the device
  private int id;

  public MyFragment() {
    setRetainInstance(true); // this will prevent the app from crashing on screen rotation
  }

  public MyFragment(Resources resources, int id) {
    this();  // this makes sure that setRetainInstance(true) is always called
    this.resources = resources;
    this.id = id;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ImageView imageView = (ImageView)inflater.infale(R.layout.fragmentview, container, false);
    imageView.setImageBitmap(BitmapFactory.decodeResource(resources, id));

    return imageView;
  }
}

This is pretty much off the top of my head, so it might not be exact, but it is pretty close.

If you need any more help just shout, don't forget to mark this answer as having helped you:).

Sheetal,

As requested, I use the above code as follows:

In my FragmentActivity I do the following in the onCreate() method:

pager = (ViewPager)findViewById(R.id.pager);
imageAdapter = new MyPagerAdapter(this, pager, theResources, listOfIds.pageBitMapIds);
imageAdapter.setRecentListener(this);
pager.setAdapter(imageAdapter);

Then in my PagerAdapter I do the following:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    MyFragment myFragment = new MyFragment(resources, resourceIds[position], recentListener, position);
    myFragments[position] = myFragment;
    return super.instantiateItem(container, position);
}

@Override
public Fragment getItem(int position) {
    return myFragments[position];
}

And then I use the code in my previous answer above.

This is obviously not complete code and it has been adapted from my actual code, but it should give you a good idea of what to do and where to do it.

Mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM