Apparent memory leak with image resize
See original GitHub issuePrerequisites
- 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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
orGLIBC_TUNABLES=glibc.malloc.trim_threshold=100000
as mentioned in the comment above resolved my issue.Ping @shadowndacorner ?