Memory leak when using Magick.NET on AWS EC2 instance

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
andyfurniss
Posts: 1
Joined: 2018-07-22T13:40:01-07:00
Authentication code: 1152

Memory leak when using Magick.NET on AWS EC2 instance

Post by andyfurniss » 2018-07-22T13:44:39-07:00

I'm using the Magick.Net-Q16-AnyCPU nuget package for resizing images that are first downloaded. On my locally machine this runs perfectly fine without problems.

I have deployed this tool (as a console app) and it is automatically run on a schedule on an AWS EC2 instance. On the EC2 instance, the tool gradually eats up all the RAM until there is none left and then crashes for obvious reasons.

I really don't understand why this would happen on the EC2 instance but not on my machine. This makes me think that it's not a code issue but rather an environment issue. I have included my code below regardless.

Code: Select all

private void resizeVariant(ImageVariant imageVariant, byte[] data)
{
	using (var image = new MagickImage(data))
	{
		image.Format = MagickFormat.Jpeg;
		image.Interlace = Interlace.Jpeg; // Make image progressive

		var geometry = new MagickGeometry(imageVariant.Width, imageVariant.Height);
		geometry.FillArea = true;

		try
		{
			image.Resize(geometry);
			image.Crop(imageVariant.Width, imageVariant.Height);
			image.RePage();

			imageVariant.ResizedImageData = image.ToByteArray();
		}
		catch (Exception ex)
		{
			this.logger.Error($"Error resizing image variant for image {imageVariant.S3Key} ({imageVariant.Width},{imageVariant.Height})\n{ex.Message}\n{ex.StackTrace}");
			image.Dispose();
		}

		image.Dispose();
	}
}
I know the image.Dispose() lines are unnecessary, I added those after I discovered the memory issue to try and force the disposal.

Framework: .NET Core 2.0
EC2 instance type: Microsoft Windows Server 2016

User avatar
fmw42
Posts: 24121
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory leak when using Magick.NET on AWS EC2 instance

Post by fmw42 » 2018-07-22T17:15:23-07:00

What are you resource limitations on your EC2 palatform? You can get those from the command line:

Code: Select all

convert -list resource
Does it show any restrictions smaller than you expect?

See http://www.imagemagick.org/script/resources.php for information about your policy.xml file and possible locations.

Sorry, I know little about Magick.NET and Windows.

Post Reply