Reinherd
Reinherd

Reputation: 5506

When can I recycle a Bitmap?

I'm having serious issues with memory, and I'm looking forward to recycle bitmaps.

However, what my app is doing atm, is building a Gallery (yeah, the deprecated) with several bitmaps, and then, several Galleries.

So at the end of the day, the app looks like (LinearLayout style):

*Some webviews *A Gallery with 7 images. *Some webviews *A Gallery with 10 images

and so on.

So what I'm thinking is... Once I've displayed those images, and them are on the screen, can those bitmaps be recycled?

Is there any way to recycle a whole Gallery component?

Thank you so much.

Edit:

I've tried soo many things. I'm still getting the error java.lang.IllegalArgumentException: bitmap size exceeds 32bits

This is my actual code:

BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 
        bfOptions.inDither=false;          //Disable Dithering mode
        bfOptions.inPurgeable=true;       //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        bfOptions.inInputShareable=true;  //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        bfOptions.inTempStorage=new byte[32 * 1024]; 
        bfOptions.inInputShareable=true;
        bfOptions.inPreferredConfig = Bitmap.Config.RGB_565;    



        Bitmap bitmap = BitmapFactory.decodeFile(mUrls.get(position).getPath(), bfOptions);

        inflatedImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));

As you can see, I've set many options to the Bitmap, aswell as resizing it, and lowering it's quality. Still happenning the same issue.

Upvotes: 0

Views: 220

Answers (2)

Reinherd
Reinherd

Reputation: 5506

Ok. I solved the issue I was having; I had a linearLayout with gallery components and webviews. If I didn't render the webviews the app didn't crash.

So after some tweaks now it's working correctly without crashes:

wv_detail.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);      
wv_detail.setLayerType(View.LAYER_TYPE_NONE, null);

Upvotes: 1

w.donahue
w.donahue

Reputation: 10886

First you should never manually call system.gc, and generally there is no need to manually recycle bitmaps either. Let the operating system do what its best at, deciding when to take the huge hits to run garbage collection.

One of the reasons that gallery was deprecated is I am fairly sure it just loads all items as soon as it renders. And does not load them and recycle them on demand like a list view. Which means it is a huge waste of RAM and no amount of recycling will help you. In addition Gallery is very glitchy on 4.0+ phones, which is the majority of the Android user base at this point. I would strongly recommend you move away from using gallery.

There are two alternatives.

1) View Pager which is now built into the Support V4 library

2) HorizontalListView --> https://github.com/MeetMe/Android-HorizontalListView

Also be absolutely sure you are downsampling all images prior to loading them in the image views. To learn how read http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Upvotes: 1

Related Questions