Memory leak when using Magick.NET on AWS EC2 instance
Posted: 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.
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
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();
}
}
Framework: .NET Core 2.0
EC2 instance type: Microsoft Windows Server 2016