Page 1 of 1

Memory leak when using Magick.NET on AWS EC2 instance

Posted: 2018-07-22T13:44:39-07:00
by andyfurniss
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

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

Posted: 2018-07-22T17:15:23-07:00
by fmw42
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.