2

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.

shivani gupta
  • 235
  • 4
  • 20

2 Answers2

3

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.

Community
  • 1
  • 1
Abhishek
  • 2,350
  • 2
  • 21
  • 35
  • Before picasso i was using Universal Image Loader library,but still it caused out of memory. so then i switched to picasso but the crashing is still there @avi – shivani gupta Jul 10 '15 at 07:33
  • Convert Your image to bitmap and load image efficiently http://developer.android.com/training/displaying-bitmaps/load-bitmap.html and check this url too Strange out of memory issue while loading an image to a Bitmap object – Abhishek Jul 10 '15 at 07:39
  • How can i acheive this with imageloader – shivani gupta Jul 10 '15 at 10:20
  • hi shivani, this is a blog http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en .. check this link.. he explain and introduce the difference in glide and picsso. step by step. – Abhishek Jul 10 '15 at 10:55
  • I am trying glide like this Glide.with(c).load(AppConst.BASE_IMAGE_URL+p.getCover()).into(holder.imageView); But memory error is still there – shivani gupta Jul 10 '15 at 11:09
  • How large is your image? If you have problems with all image loading libraries, the image might just be too large to be processed. If you use Picasso, [resize it before you display it](https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit/). I'd recommend to try to use `.fit()` and see what happens. – peitek Jul 10 '15 at 11:18
  • @peitek i resized the image to 150*150 in picasso still i was getting the error .Don't know where i am wrong in using the libraries and fit() also doesn't work – shivani gupta Jul 10 '15 at 11:21
  • @shivanigupta first go though that blog. you will see the difference. – Abhishek Jul 10 '15 at 11:22
  • 1
    @shivanigupta are you sure you're not leaking any other (java) objects, which slowly fill up your heap? Maybe Picasso/Glide just cause the final crash with their need with the bitmaps, but the ultimate cause are other memory leaks? I've not experienced any out-of-memory with Picasso and images that small. Especially if it takes 30 minutes of using the app to crash it. Maybe looking into the memory performance of your app helps: https://developer.android.com/tools/debugging/debugging-memory.html – peitek Jul 10 '15 at 11:32
  • I used Glide to load images instead of imageloader and also applied single ton pattern in volley and the issue got resolved.Thanks @peitek for help :) – shivani gupta Jul 20 '15 at 10:59
  • Thanks to @abhishek for helping – shivani gupta Jul 20 '15 at 10:59
1

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.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
  • i just wanted to thank you for your comments about Fresco i had forgotten about it and was using picasso. i was haivng out of memory issues. switching to fresco and using android:largeHeap="true" setting in the manifest really helped me. – j2emanue Jul 03 '17 at 13:09