title: OutOfMemoryException using Bitmaps in Xamarin.Android
body: <p>I'm facing an OutOfMemoryException when I try to download images (that can be large images). I'm working with Xamarin.Android and a PCL for cross-platform actions.</p>
  
  <p>I want to do an image slideshow. I have a limited number of imageviews superposed on my layout. When I have swipe out all images, I reload new images in those imageviews. I've made a simple project with just the refreshing part of the mechanism.</p>
  
  <p>Please, be nice, I'm a beginner in Android and Xamarin, here is the code :</p>
  
  <p>MainActivity.cs:</p>
  
  <pre><code>[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity
      {
          private string[] urls = {
                                      "http://momotaros.fr/wp-content/uploads/2014/01/One-Piece-5.png",
                                      "http://www.infinite-rpg.com/wp-content/uploads/2014/09/Equipage.png",
                                      "http://ekladata.com/ke33JLPQ2tTW1mTHCvPJBrKCPOE.jpg",
                                      "http://ekladata.com/P2Q1mQuYTDbfEZnwb3f3IBsXoGM.png",
                                      "http://ekladata.com/9QUE66iKU9uGUPVUdFmPF_ZIkK8.png"
                                  };
          protected override void OnCreate(Bundle bundle)
          {
              BlobCache.ApplicationName = "App1";
              BlobCache.EnsureInitialized();
              base.OnCreate(bundle);
  
              // Set our view from the "main" layout resource
              SetContentView(Resource.Layout.Main);
  
              // Get our button from the layout resource,
              // and attach an event to it
              Button button = FindViewById&lt;Button&gt;(Resource.Id.MyButton);
              ImageView image = FindViewById&lt;ImageView&gt;(Resource.Id.imageView1);
              ImageView image2 = FindViewById&lt;ImageView&gt;(Resource.Id.imageView2);
              ImageView image3 = FindViewById&lt;ImageView&gt;(Resource.Id.imageView3);
              ImageView image4 = FindViewById&lt;ImageView&gt;(Resource.Id.imageView4);
              ImageView image5 = FindViewById&lt;ImageView&gt;(Resource.Id.imageView5);
              button.Click += (sender, e) =&gt;
              {
                  image.SetImageURL(urls[0]);
                  image2.SetImageURL(urls[1]);
                  image3.SetImageURL(urls[2]);
                  image4.SetImageURL(urls[3]);
                  image5.SetImageURL(urls[4]);
              };
          }
      }
      public static class BitmapExtensions
      {
          public static async void SetImageURL(this ImageView imageView, string url)
          {
              IBitmap bmp = await App1.Shared.ImageDownloadService.DownloadImage(url, imageView.Width, imageView.Height);
              if (bmp != null)
              {
                  imageView.SetImageDrawable(bmp.ToNative());
              }
          }
      }
  </code></pre>
  
  <p>ImageDownloadService.cs (in a PCL):</p>
  
  <pre><code>public class ImageDownloadService
      {
          public static async Task&lt;Splat.IBitmap&gt; DownloadImage(string url, float desiredWidth, float desiredHeight)
          {
              return await BlobCache.LocalMachine.LoadImageFromUrl(url,false, desiredHeight:desiredHeight,desiredWidth: desiredWidth);
          }
      }
  </code></pre>
  
  <p>When the button is clicked the 1st time it's OK, images are downloaded (even if I found memory usage a little bit high in DDMS).</p>
  
  <p>But for next clicks, watching over memory usage, it increases like hell.</p>
  
  <p>I was thinking that previous bitmaps in memory are not disposed when I set a new image in that imageView, kind of a strong reference somewhere but I can't find out where it is if it's that.</p>
  
  <p>I would appreciate your help for this problem or any techniques for debugging memory usage like something to track where an object is created and where it's is destroy.</p>
  
  <p>Thanks for your time reading this post, and I hope you can help me out.</p>