This project has moved. For the latest updates, please go here.

out of memory exception thrown

Nov 2, 2016 at 6:56 AM
Edited Nov 2, 2016 at 8:23 AM
Hi,

Im trying to run magick image in a multiple worker threads but it is occasionally throwing a out of memory exception despite there should be plenty of memory available.

I am using q16 any cpu version 7.0.3.501 and I am trying to do the following on 4096 x 4096 images
        MemoryStream outputStream = new MemoryStream();
        try
        {
            using (ImageMagick.MagickImage img = new ImageMagick.MagickImage(input))
            {
                img.ColorAlpha(backgroundColor);
                img.Format = ImageMagick.MagickFormat.Pjpeg;
                img.Quality = quality;
                img.Strip();
                img.Write(outputStream);
            }
        }
        return outputStream;
I know the images are big and that they take up around 128 mb in raw format.
I run 4 thread concurently and the used application memory goes between 700mb and 1200mb according to the task manager.

I am setting the ImageMagick.ResourceLimits.Memory = 16009916416 so that should not be the issue (while it is running I have around 7gb of free and available memory according to the task manager).

I also noticed that it saved a lot of image magick files to the temp directory which I was hoping to avoid as 128 mb of diskspace per image is rather much.

Any advice is appriciated
Coordinator
Nov 2, 2016 at 9:37 AM
Are you running your application in 32-bit or 64-bit mode? What kind of application do you have.
Nov 2, 2016 at 4:08 PM
Hi dlemstra,

Thanks for the quick feedback. The application was indeed targeting x86 hence the issue with the memory.
Targeting x64 specifically solved the issue.
Coordinator
Nov 2, 2016 at 6:14 PM
Happy to hear that you got it resolved and thanks for coming back to tell us how you fixed it.
Nov 3, 2016 at 8:29 PM
Edited Nov 3, 2016 at 8:32 PM
Unfortunately this only solved my issue with not using all the available ram.

There seems to be a serious memory leak in the magick.net library.

As I wrote then I am running it in a multithread scenario and I have made a small component (managed dll) that encapsulates magick image (nquant and image processor libraries).
From my main application (which is where the threads are running) I am calling a function that encapsulate the code I pasted in the first post.

My problem is that the memory consumed by magick.net just keeps increasing and increasing. when it hit the maximum allowed memory then it starts filling the %temp% folder with image magick raw images. Some of these files are deleted but not all which means that eventually this fills the entire hd or maximum allowed space.

Both the managed dll and the main application is specifically target for x64 CPU's/OS's. I have tried to explicitly call dispose on the MagickImage in the code from above, but with no change. I have also removed the Strip() function to see if that should have any influence and it did not.

The reason why I am relatively certain that this issue is related to the magick.net library is that I tried to switch to the ImageProcessor library to do the same thing with the following code:
        using (ImageFactory imageFactory = new ImageFactory())
        {
            // set to beginning of stream
            input.Seek(0, SeekOrigin.Begin);
            // create output stream
            MemoryStream outputStream = new MemoryStream();
            // apply quality setting and output to given image
            imageFactory.Load(input).Format(new JpegFormat()).BackgroundColor(backgroundColor).Quality(quality).Save(outputStream);
            // return the output stream
            return outputStream;
        }
And with the ImageProcessor library there were no memory issues at all. The only difference from the main application is the method that is called in my managed dll. Unfortunately the ImageProcessor library does not give as good image quality as image magick so I would prefer not to switch to it permanently.

Hope someone is able to help!
Coordinator
Nov 3, 2016 at 11:35 PM
It looks like there is a memory leak in the ColorAlpha method. Does the memory issue disappear when you don't call that method? I will fix the memory leak tomorrow and publish a new release.
Nov 4, 2016 at 5:47 AM
Hi Dlemstra,

Yes not calling that function seems to solve the issue.
Unfortunately I cannot avoid calling it. Looking forward to the new release.

//perlohmann
Coordinator
Nov 4, 2016 at 9:29 PM
The new release has been publish. Have fun :)
Nov 4, 2016 at 9:51 PM
Great!
Thx for the quick turn around time