title: Download an image using the least amount of memory
body: <p>I'm developing an app for WP8. In a <code>ScheduledTaskAgent</code> I need to download an image from internet, save it to storage, after that I load the same image and do some processing. I am facing the dreaded <code>OutOfMemoryException</code>. If I remove the "download and save image" (using an already saved image) part, it all works. If I just download and save, it all works. Maybe in the first schedule I can save it, in the second process. But I do not understand where or why my download image is holding memory so, can anyone suggest me a better way to do this? Here is my actual code:</p>
  
  <pre><code>    private async Task GetImage(string url)
      {
          using (var http = new HttpClient())
          {
              var clientResponse = await http.GetByteArrayAsync(url);
  
              var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(lockFilename, Windows.Storage.CreationCollisionOption.ReplaceExisting);
              using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
              {
                  var outStream = fs.GetOutputStreamAt(0);
                  using (var dataWriter = new Windows.Storage.Streams.DataWriter(outStream))
                  {
                      dataWriter.WriteBytes(clientResponse);
                      await dataWriter.StoreAsync();
                      dataWriter.DetachStream();
                      await outStream.FlushAsync();
                      outStream.Dispose(); 
                      fs.Dispose();
                  }
              }
          }
      }
  </code></pre>