marczulajtis marczulajtis - 1 year ago 88
C# Question

Magick.NET C# - huge memory usage

I'm struggling with Magick.NET library, as when converting image files to pdf's my memory usage is over 4GB and the CPU usage is 100%. When the conversion is done it all backs to normal. but as I'm using this particular third party in many instances of one application it causes huge memory loss.

Problem exists on line images.Write(newPdfPath);

using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(orginalImage);

images.Write(newPdfPath);
}


The images are different sizes, and it really doesn't matter how big, as when converting jpg of size 7 KB the issue also exists.

Please help!!!!

Answer Source

the cpu and memory allocations are not related to the size of the image on your hard drive. It completly related to the number of pixels you have. if you have a completely white image of 20,000 pixel by 20,000 pixel, the size of this file on your hard drive can be 6 MG but when you load it into memory with Magick.net it will be gigabytes. So first you have to see what size(in pixels) are the images and then we can judge about the performance. Then you can use these approaches to improve the performance:

once you load the image into memory you can write it on HDD with .mpc format and then you can load it into memory very fast. (if you need to load couple of times your images)

Use Magikc.net q8 instead of q16

if you can run the command on a parallel loop then Magick.net version 7 can run almost 4 times faster.

and as the other answer is saying you have to dispose your image when it has been done.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download