Bitmap Utilization Does Not Free Memory

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); } 
+7
android memory-leaks bitmap xamarin xamarin.android
source share
2 answers

At the end of the java.lang.System.gc() call, after deleting the images, he did the trick.

+7
source share
 using (var imageView = FindViewById<ImageView>(Resource.Id.imageView1)) using (var bitmap = Android.Graphics.BitmapFactory.DecodeResource( this.Resources, Resource.Drawable.Icon)) imageView.SetImageBitmap(bitmap); 

I saw this approach in several places, the other is using WeakReference for ImageView. These approaches can help GC properly assemble the link to ImageView.

+1
source share