0

I'm working on a project that uses a series of image urls and loads them into a recyclerview grid. The images can be any resolution and the app crashes after loading just 6 images. I thought using Picasso would solve the problem but that didn't work. The app is now slower and recyclerview scroll is laggy while it loads only 3-4 images at max before crashing.

Memory Leaks:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    LayoutInflater inflater;

    public RecyclerViewAdapter(Context c){
        inflater=LayoutInflater.from(c);
    }

    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.recyclerview_item,parent,false);
        itemView.setMinimumHeight(150);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        if(urls!=null){
        //Uri uri = Uri.parse(urls.get(position));
        //Toast.makeText(getContext(),""+urls.get(position),Toast.LENGTH_SHORT).show();
        Picasso.with(context)
                .load(urls.get(position))
                .into(holder.view);
        }
    }

    @Override
    public int getItemCount() {
        if(urls != null)
            return urls.size();
        else
            return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ImageView view;

        public ViewHolder(View itemView) {
            super(itemView);
            view=itemView.findViewById(R.id.image_recycler);
        }

        @Override
        public void onClick(View v) {

        }
    }
}
Aditya Nigam
  • 869
  • 2
  • 11
  • 21
  • 1
    OOM problems are a whole app problem, not usually the immediate cause. You need to look at memory usage over your whole app. – Gabe Sechan Mar 12 '18 at 22:51
  • @GabeSechan u mean its not about having too many images to load? well could u suggest what else could be the problem? – Aditya Nigam Mar 12 '18 at 22:55
  • It may be. It may not. Its impossible to tell from the given info. OOM means the total amount of memory you use is too high. Images are large contibutors to that, but it could also be memory leaks, large objects, inefficient data structures, or other things. – Gabe Sechan Mar 12 '18 at 23:01
  • What is the size of the images that you are loading? – Napster Mar 12 '18 at 23:11
  • @Napster it can be any- small or large. really depends on the source from where the user picks it. Largest of the uploads I have in the storage right now is 1.5 MB. – Aditya Nigam Mar 12 '18 at 23:15
  • Can you post the `RecyclerView` code or anything else that might be leaking memory? – Napster Mar 12 '18 at 23:20

3 Answers3

0

try Glide library. it's really efficient: https://github.com/bumptech/glide

Soroosh
  • 543
  • 1
  • 4
  • 21
  • https://stackoverflow.com/questions/29363321/picasso-v-s-imageloader-v-s-fresco-vs-glide I don't really find any difference. – Aditya Nigam Mar 12 '18 at 22:58
0

This is from Picasso official website, it'll reduce the image size. If this works then the problem is that your images sizes are too big

You can also specify custom transformations for more advanced effects.

public class CropSquareTransformation implements Transformation {
  @Override public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
    if (result != source) {
      source.recycle();
    }
    return result;
  }

  @Override public String key() { return "square()"; }
}

Pass an instance of this class to the transform method.

y.allam
  • 1,446
  • 13
  • 24
  • Try not to use such large sized images, they will take more time to download and will consume more memory – y.allam Mar 13 '18 at 14:16
0
 <application
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme"/>

Use largeHeap="true" in manifest file.