Reputation: 235
I am using picasso library for loading multiple images on grid from server. I have three fragments on main activity.Each fragment loads multiple images on grid from server using picasso. when i navigate from fragment to fragment continously ,the fragments loads slow and then after half hour app get crash due to out of memory error in picasso. How to resolve it?
public class MyAlbImageAdapter extends BaseAdapter {
public List<MyAlbum> _albumList=AppController.getInstance().getPrefManger().getMyAlbums();
public static int flag=0;
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageLoaderConfiguration config;
ImageLoader imageLoader;
Context c;
public MyAlbImageAdapter(Context context,List<MyAlbum> album)
{
_albumList=album;
inflater = LayoutInflater.from(context);
this.c=context;
}
@Override
public int getCount() {
return _albumList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if(v == null)
{
v = inflater.inflate(R.layout.myalb_outer_griditem, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.thumbnail);
holder.t1 = (TextView) v.findViewById(R.id.alb_name);
holder.t2 = (TextView) v.findViewById(R.id.usr_name);
v.setTag(holder);
}
else
{
holder = (ViewHolder) v.getTag();
}
MyAlbum p = _albumList.get(position);
holder.t1.setText(p.getName());
Picasso.with(c).load(AppConst.BASE_IMAGE_URL+p.getCover()).fit().centerCrop().into(holder.imageView);
return v;}}
Please help, i tried many links on net but didn't find anything useful.
error is:
07-10 12:40:46.230: E/dalvikvm(17680): Out of memory: Heap Size=131107KB, Allocated=129839KB, Limit=65536KB
07-10 12:40:46.240: E/dalvikvm(17680): Extra info: Footprint=131043KB, Allowed Footprint=131107KB, Trimmed=1452KB
07-10 12:40:46.240: E/Bitmap_JNI(17680): Create Bitmap Failed.
07-10 12:40:46.240: E/Bitmap_JNI(17680): Failed to create SkBitmap!
07-10 12:40:48.012: E/dalvikvm-heap(17680): Out of memory on a 198896-byte allocation.
Upvotes: 2
Views: 7517
Reputation: 3692
Try Fresco library from Facebook. Earlier, I was also using Picasso and used to get OOM error as the heap size allocated to an app is very small and can quickly become full if you are loading images of large size. Fresco uses ashmen cache which allows for much more data to be cached. It also has an option of downsampling the images and progressive loading of JPEGs. I have not faced any OOM issues since I switched to Fresco. Do try it. Also, try to use OkHttp as the networking library. It provides http caching which might help too. Plus enable largeHeap as written by @codephillip. That helps a bit too.
Upvotes: 1
Reputation: 2370
Similar ques. Android Picasso ImageView - Out of Memory Exception MemoryLeak
When Android "unwraps" your image (i.e. decodes it to bitmap), it will use 4 bytes per pixel. Count the number of pixels, multiply that by 4 and then by 20 (number of your images) and you'll probably get close to the 100mb figure. For instance, if your images have 1,000,000 pixel resolution, it would be 1,000,000 x 4 x 20 = 80mb.
Use some kind of LRU cache or similar (or alternatively use Universal Image Loader library which handles caching for you) and only load your bitmaps when you need them.
Upvotes: 3