title: Release/Reduce ImageSharp Memory Usage After Use
body: <p>I have created a test WPF application for the testing of SixLabors.ImageSharp to see how it performed and if it met some requirements etc before any integration into a production application.</p>
  <p>My test app consists of an Image control to display the image that will be maniplulated and a few buttons to load an image, rotate, apply rotation.</p>
  <p>When the app launches, it consumes roughly ~30-40MB of system memory.</p>
  <p>However, as soon as I apply a rotation to the image using the below code and save back to disk the memory usage goes from it's ~25-40MB to ~300-500MB.</p>
  <p>If I apply a second rotation to the image, the memory usage goes up again to somewhere around ~600-800MB.</p>
  <p>If I apply a third rotation to the image, the memory usage goes up again to somewhere around ~850-1,000MB.</p>
  <p>If I apply a fourth rotation to the image, the memory usage drops to around where the first rotation was performed, somewhere ~300-500MB.</p>
  <p>I am specifying that the application uses MinimalPooling to attempt to keep the memory usage down.</p>
  <pre><code>Configuration.Default.MemoryAllocator = ArrayPoolMemoryAllocator.CreateWithMinimalPooling();
  </code></pre>
  <p>Rotation code</p>
  <pre><code>private void ApplyRotation()
  {
      string output;
  
      using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageToProcess))
      {
          // Decide based on the angle which rotation to use/
          // Rotate the image in place.
          // 'x' signifies the current image processing context.
  
          switch (angle)
          {
              case 0:
              case 360:
  
                  break;
  
              case 90:
                  image.Mutate(x =&gt; x.Rotate(RotateMode.Rotate90));
  
                  break;
  
              case 180:
                  image.Mutate(x =&gt; x.Rotate(RotateMode.Rotate180));
  
                  break;
              case 270:
                  image.Mutate(x =&gt; x.Rotate(RotateMode.Rotate270));
  
                  break;
  
          }
  
          // The library automatically picks an encoder based on the file extension then
          // encodes and write the data to disk.
          // You can optionally set the encoder to choose.
          output = imageToProcess;
  
          image.Save(output);
      }
  }
  </code></pre>
  <p>Looking at the <a href="https://docs.sixlabors.com/articles/imagesharp/memorymanagement.html" rel="nofollow noreferrer">ImageSharp Documentation</a> it states that ImageSharp retains roughly 300-400MB of memory due to their use of ArrayPools. This explains the first memory jump when the library is first used, however the subsequent jumps in memory usage I don't understand or know how to release.</p>
  <p>I have used</p>
  <pre><code>Configuration.Default.MemoryAllocator.ReleaseRetainedResources();
  </code></pre>
  <p>as found in their documentation which I believe should release the resources that are being held onto. This does not seem to be the case. If I apply 3 rotations to my image and the memory usage is sat around 1GB, at no point in the application running will this reduce back to less than 300MB, even if the controls are being used on another window and then closed afterwards.</p>
  <p>Does anyone know how to reduce this memory usage or dispose of eveyrthing so the memory usage returns to normal.</p>
  <p>Thanks</p>