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

Memory Leak in Creating MagickImage Object From MPC Files

Jan 9 at 10:36 AM
Edited Jan 9 at 10:41 AM
I had a problem with memory leak in our system for couple of months. I was searching and investigating whenever I was less busy during these months. This week I focused on this issue and I found it. I believe it is related to MagickImage object. I build a whole new MVC application to test it in a fresh environemt.
here is the code in the controller that creat 200 MagickImage from mpc file which is created with the same MagickNet version.
    public JsonResult BuildMagickFromCache()
    {
        for (int i = 0; i < 200; i++)
        {
            using (var myMagickImage = new MagickImage(@"D:\temp\test.mpc"))
            {
                int j = i;
                j++;
            }

        }
        return new JsonResult { Data = "done" + DateTime.Now.Second.ToString(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };

    }
everytime I hit this action it added about 250 Mg into the memory and memory increases over time. so if i hit that action for 4 times my memory is over 1 Gig.
I called GC.Collect() after the loop but no changes in term of memory usage. I called dispose of the MagickImage objects but still there was memory leak.

I wrote a console application in c# to test it in a exe program. This time memory increases very little every time. for example couple of Mg not hunderends of Mg . However the graph of memory usage still is increasing. Is there any settings or configuration ? or it is a bug in creating MagickImage from mpc files?
    static void Main(string[] args)
    {          
        bool continueThisLoop = true;

        while (continueThisLoop)
        {

            for (int i = 0; i<200; i++)
            {
                using (var mymagick = new MagickImage(@"D:\temp\test.mpc"))
                {
                    int b = 9;                       
                }
            }


            var read = Console.ReadLine();
            if (read == "exit")
                continueThisLoop = false;
        }
    }

*I dont now why the plus plus is converting to that code. I tried to edit it but it is not fixed.

I use :
MagickNet 7.0.3.500 any cpu 8
Visual studio 2015
IIS 10
Coordinator
Jan 9 at 11:27 AM
This is most likely be a memory leak inside the ImageMagick library. Would you mind testing this with the latest release? If you can still reproduce this I will investigate this. And can you reproduce this with any MPC file? Would you mind sharing your test.mpc file? Feel free to contact me through CodePlex if you don't want to share it publicly.
Jan 9 at 11:51 AM
I reproduced it with different mpc files. And I reproduced it with the latest version of Magick.Net
I put the .mpc/.cache and the source jpg I used to create the mpc file (mpc has been created with the latest version pf Magick.Net:7.0.4.100 ) in a zip file and is here available for download:

https://ufile.io/def76

if you prefer other way to share the mpc file, please let me know and I will share it in that way.

Thanks,
Meysam
Coordinator
Jan 9 at 12:42 PM
Thanks for your reply. The download site looks weird but I should be able to find the correct link :) I'll take a look at it tonight when I am back home.
Jan 9 at 12:56 PM
If you could not download the file you can create your own mpc while this issue is reproduceable with any mpc. but of you want to test please create big mpc (over one megabytes) to see the increase in memory sharply.
Thanks alot,
Meysam
Jan 10 at 10:50 AM
I did lots of testing in different projects . It seems that you right that the memory leak is inside the ImageMagick library . I used Ants Memory Profiler and it showed me that 99 percent of the memory my application is using is related to unmanaged code, and not dot net; the only library I have installed is the Magick.Net so something should be in ImageMagick.
Did you have a look into this ?
Coordinator
Jan 10 at 2:13 PM
I haven't had a change to look at it last night. Will try to take a look tonight but I also have some other stuff scheduled. But I can promise it will be this week :)
Coordinator
Jan 11 at 5:25 AM
Edited Jan 11 at 7:04 AM
The leak has been found and fixed. This will be resolved in the next version of Magik.NET that I will publish this weekend.
Jan 11 at 9:23 AM
Thanks a lot.

I just wondering that the leak was in Magick.net or in ImageMagick?
Coordinator
Jan 11 at 9:27 AM
The leak was inside the MPC reader of ImageMagick.