title: Application runs out of memory while saving textures to PNG files
body: <p>I'm using SaveAsPng() on a RenderTarget2D object every frame. I dispose of both the RenderTarget2D and the input stream on every frame too. Yet, just before about 500 images saved I get a message saying the application ran out of memory.</p>
  
  <p>I tried changing around when I call Dispose(), but I got the same result every time.</p>
  
  <pre><code>RenderTarget2D rt;
  
  protected override void Update(GameTime gameTime)
      {
      if (rt != null)
          {
          rt.Dispose();
          }
  
      rt = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
  
      GraphicsDevice.SetRenderTarget(rt);
      GraphicsDevice.Clear(Color.Transparent);
  
      sb.Begin();
      //I draw stuff here.
      sb.End();
  
      GraphicsDevice.SetRenderTarget(null);
      GraphicsDevice.Clear(Color.Black);
  
      Stream stream = File.Create(path);
      rt.SaveAsPng(stream, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
      stream.Dispose();
      stream.Close();
      }
  </code></pre>
  
  <p>What I expect this to do is release the memory associated with the picture drawn on the previous frame, assign new memory space to the new RenderTarget2D and as such, only use as much memory as to store a single one of the images I'm drawing. For some reason, I think, all the images are stored in memory until space runs out. (The error is generated by the SaveAsPng method call, memory usage seems to go up linearly with run time according to VS and nothing in the application, other than this little image processing bit uses large chunks of memory.)</p>
  
  <p>Update:
  This is my first time ever using memory snapshots, so I must be looking at this wrong, but to me it looks like the data on the left doesn't seem to reflect the memory usage graph on the right.</p>
  
  <p><a href="https://i.stack.imgur.com/CKLVm.png" rel="nofollow noreferrer">Snapshot Comparison</a></p>