-1

I do have an app that shows images and some other text info from firebase-Realtime, there is a problem with my app ram usage that it can easily cross over 400+mb so i used profiler to check and solved every MemoryLeaks my app have, now when i checked the Android studio profiler i found that there is a ton of Bitmaps that are using loads of memory.

AlSO Iam using Picasso to load images into Recycleview from within Adapter

also using Glide sometimes

so i want to knew is it possible to solve these bitmap memory usage or is it possible to to limit number of bitmap that can be used, cuz every time a new image are shown the app would make a new bitmap and uses more Ram.

Adapter code to load Images into recyclerview:

public class HomeScreenWorkViewHolder  extends RecyclerView.ViewHolder {

    View view;
    DatabaseReference likeReference;
    public ImageView like_btn;
    public TextView like_text;
    public HomeScreenWorkViewHolder(@NonNull View itemView) {
        super(itemView);

        like_btn = itemView.findViewById(R.id.like_btn);
        like_text = itemView.findViewById(R.id.like_text);
        view = itemView;
    }
    public HomeScreenWorkViewHolder(@NonNull View itemView, OnItemClick callBack) {
        super(itemView);

        like_btn = itemView.findViewById(R.id.like_btn);
        like_text = itemView.findViewById(R.id.like_text);

        view = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onItemClicked(getAdapterPosition());
            }
        });
    }
    public void setdetails( String name, String image, String description, String location) {
        TextView mtitletv = view.findViewById(R.id.product_layout_name);
        TextView mdesrcriptiontv = view.findViewById(R.id.product_layout_description);
        TextView mlocationtv = view.findViewById(R.id.product_layout_location);
        ImageView mImagetv = view.findViewById(R.id.product_layout_image);


        mtitletv.setText(name);
        mdesrcriptiontv.setText(description);
        mlocationtv.setText(location);
        Picasso.get().load(image).placeholder(R.drawable.ic_baseline_cloud_download_24).into(mImagetv);
    }

profiler enter image description here

enter image description here

MiniDev99
  • 324
  • 1
  • 13

1 Answers1

1

Sounds like you are either keeping references to the bitmap around. If that is not the case, but you are trying to keep references in memory, but reduce memory footprint with changing the sample size.

Sampling size can be thought of as this: If you have a 1000x1000 image by 8 bits per pixel.. You have a 1,000,000 bytes of image loaded into memory. Lets say you only needed a thumbnail of 100x100. You could then change the sampling size to 10, and it would read every 10th pixel from the file, building a newer / smaller memory footprint image. This would then Go from 1,000,000 bytes to 10,000 bytes.

Try the answer on this SO question..

Strange OutOfMemory issue while loading an image to a Bitmap object

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeStream(is, null, options);
Chrispix
  • 17,941
  • 20
  • 62
  • 70