marczulajtis marczulajtis - 2 years ago 125
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())


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 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 q8 instead of q16

if you can run the command on a parallel loop then 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