question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apparent memory leak with image resize

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
kaktuspalmecommented, Feb 24, 2021

Maybe someone will stumble across the same problem as me. I’m using Magick.NET in a ASP.NET Core application inside a linux docker container. An image conversion is triggered by a rabbitmq message. What happened was that the application was using much more memory than it should. After analysing memory and searching for leaks etc. I finally stumbled across this: https://github.com/dotnet/runtime/issues/13301#issuecomment-535641506

Setting the environment to either MALLOC_TRIM_THRESHOLD_=100000 or GLIBC_TUNABLES=glibc.malloc.trim_threshold=100000 as mentioned in the comment above resolved my issue.

1reaction
dlemstracommented, Jan 1, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak when resizing images using CoreGraphics on ...
I'm building an app that uploads resized versions of many images to the server, and I seem to have a memory leak that...
Read more >
Apparent memory leak - InkscapeForum.com
The item is gone but my file size is still 5mb. Ive pasting in images, taking pictures and then deleting and modifying what...
Read more >
Fixing memory leaks in web applications | Read the Tea Leaves
One of these problems is memory leaks. A poorly-coded SPA can easily eat up megabytes or even gigabytes of memory, continuing to gobble...
Read more >
Hunting Java Memory Leaks
In this post, I'll explain how and why memory leaks occur in Java and outline an approach for detecting such leaks with the...
Read more >
4 Types of Memory Leaks in JavaScript and How to Get Rid ...
In this article we will explore common types of memory leaks in client-side JavaScript code. We will also learn how to use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found