Skip to content

Apparent memory leak with image resize #707

@shadowndacorner

Description

@shadowndacorner

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am using the latest version of Magick.NET
  • I have searched open and closed issues to ensure it has not already been reported

Description

My issue seems similar to this comment in #340, but it doesn't seem to have ever been addressed. It appears that MagickImage.Resize has a memory leak. I have come to this conclusion experimentally rather than via debugging the library manually, but the snipped code below does not leak memory if I comment out the image.Resize(...) line, whereas it does leak memory otherwise. I have observed this in Docker (based on the image mcr.microsoft.com/dotnet/core/sdk:2.2).

Steps to Reproduce

The container's memory balloons if I continuously make requests to the endpoint containing this code. I imagine simply calling this code in a while loop would exhibit the same behavior (I can provide the full controller if necessary, but figured I would limit it for brevity).

// ImageController.cs
var format = ...; // computed from URL

// imageData is a byte array
Debug.Log($"Creating image object...");
using (var image = new MagickImage(imageData, format))
{
    if (image.Width <= 512 && image.Height <= 512)
    {
        var str = new MemoryStream(imageData);
        Debug.Log($"Smaller than 512, returning...");
        return File(str, http.ResponseHeaders["Content-Type"]);
    }

    int targetWidth = Math.Min(image.Width, 512);
    int targetHeight = Math.Min(image.Height, 512);

    var size = new MagickGeometry(targetWidth, targetHeight);
    Debug.Log($"Resizing...");

    // Commenting out this line prevents the code from leaking memory
    image.Resize(size);

    // Save the result
    var data = image.ToByteArray();
    Debug.Log($"Resized, returning...");
    {
        var str = new MemoryStream(data);
        return File(str, http.ResponseHeaders["Content-Type"]);
    }
}

System Configuration

  • Magick.NET version: Magick.NET-Q8-AnyCPU 7.21.1.0
  • Environment (Operating system, version and so on): Docker Desktop for Windows 10, Docker on Amazon Linux 15. Container based on mcr.microsoft.com/dotnet/core/sdk:2.2
  • Additional information:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions