I have an Activity in TabHost with three other actions. Therefore, they are always alive (or are in a pause state).
The first action has four different images (~ 250 kB each), and they extract a lot of memory (about 80 MB). To indicate, I load the minimum size needed for the screen, and I use layout_weight if that helps), so I want to minimize the amount of memory required.
I already tried to delete images in OnPause state and set them to OnResume , but I had no luck, this is one example of what I was trying to do:
imageView.Drawable.Callback = null; ((BitmapDrawable)imageView.Drawable).Bitmap.Recycle(); imageView.Drawable.Dispose(); imageView.SetImageDrawable(null); imageView.SetImageBitmap(null); GC.Collect();
I don't know if uninstalling Bitmap on OnPause best strategy, but it should work. I do not understand why ImageView not going to GC (since there are no external links)
EDIT This is how I upload images. It does not work even if I put images in an xml file. Also, I don't like this code, I just want to delete the bitmap images.
void SetBackgroundImages(int imageId, int resId, float width, float height) { var imageView = FindViewById<ImageView>(imageId); using (var bitmap = DecodeSampledBitmapFromResource(Resources, resId, width, height)) imageView.SetImageBitmap(bitmap); } public static Bitmap DecodeSampledBitmapFromResource(Resources res, int resId, float reqWidth, float reqHeight) { var options = new BitmapFactory.Options {InJustDecodeBounds = true}; using (var b = BitmapFactory.DecodeResource(res,resId,options)){} options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight); options.InJustDecodeBounds = false; return BitmapFactory.DecodeResource(res, resId, options); }
android memory-leaks bitmap xamarin xamarin.android
Adam
source share