Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 729
Sheetal Suryan Avatar asked Feb 21 '12 06:02

Sheetal Suryan


2 Answers

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

    }
like image 90
Mark Avatar answered Sep 21 '22 10:09

Mark


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

like image 27
Shereef Marzouk Avatar answered Sep 21 '22 10:09

Shereef Marzouk