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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With