Magick.Net / ImageMagick - High CPU / Memory Usage

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
driventoofar
Posts: 3
Joined: 2019-11-20T11:30:17-07:00
Authentication code: 1152

Magick.Net / ImageMagick - High CPU / Memory Usage

Post by driventoofar » 2019-11-20T11:35:02-07:00

I posted this on StackOverflow but was not able to find an answer so hopefully I can find one here.

I'm running into a problem with cpu / memory usage while using Magick.Net in my server application. A single request in itself increases what is used and as more users are on combined with files that contain many pages, it can grind the server to a halt. I wonder if I am missing something in my code, if there is a way to optimize, or if I just need to limit resources.

This process pulls in a collection of byte arrays from another source with each one representing a single page. It then combines them into a single file which is returned as a byte array.

I am currently using Q16 and I'm thinking of using Q8 since it uses less resources, as long as the image quality is still good. In the long run, I may also setup another server to run this process if I can't find another solution.

Code: Select all

    private static readonly MagickReadSettings _settings = new MagickReadSettings
    {
        Density = new Density(300),
        UseMonochrome = true,
        Compression = CompressionMethod.Group4,
    };

    public static byte[] CreatePdfFromImageFiles(List<ImageFile> imageFiles)
    {
        using (MagickImageCollection col = new MagickImageCollection())
        {
            for (int i = 0; i < imageFiles.Count; i++)
            {
                byte[] data = imageFiles[i].Bytes;
                MagickImage mi = new MagickImage(data, _settings)
                {
                    Format = MagickFormat.Pdf
                };
                if (imageFiles[i].Rotation != 0)
                    mi.Rotate(Convert.ToDouble(imageFiles[i].Rotation));
                col.Add(mi);
            }

            return col.ToByteArray();
        }
    }

driventoofar
Posts: 3
Joined: 2019-11-20T11:30:17-07:00
Authentication code: 1152

Re: Magick.Net / ImageMagick - High CPU / Memory Usage

Post by driventoofar » 2019-12-03T07:34:28-07:00

Wanted to provide a response to say that using Q8 did improve the resource use without diminished quality for our situation. I am still interested to see if there is any additional settings I can use to improve the resource use.

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Magick.Net / ImageMagick - High CPU / Memory Usage

Post by snibgo » 2019-12-03T08:14:50-07:00

Have you looked at https://imagemagick.org/script/resources.php policy.xml, for example "throttle" for CPU usage? However, these impose limits on ImageMagick. I don't know what effect they would have on your compiled programs.
snibgo's IM pages: im.snibgo.com

driventoofar
Posts: 3
Joined: 2019-11-20T11:30:17-07:00
Authentication code: 1152

Re: Magick.Net / ImageMagick - High CPU / Memory Usage

Post by driventoofar » 2019-12-04T12:45:27-07:00

I tried using throttle but it seemed to make it even slower, although it's possible I had it configured incorrectly. I'm also worried that if it hits a certain threshold it's just going to make it slower for everyone else.

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Magick.Net / ImageMagick - High CPU / Memory Usage

Post by snibgo » 2019-12-04T13:37:38-07:00

"Throttle" limits the rate at which a process will consume CPU cycles. This means more processes can run without saturating the CPU (which I think you want), but an obvious side-effect is that each process will run more slowly.
snibgo's IM pages: im.snibgo.com

User avatar
magick
Site Admin
Posts: 11093
Joined: 2003-05-31T11:32:55-07:00

Re: Magick.Net / ImageMagick - High CPU / Memory Usage

Post by magick » 2019-12-04T19:03:07-07:00

Pegging a server usually is a result of a large image consuming memory resources. To prevent that, limit the memory resource so that any large images are forced to disk instead. We have a server that supports ImageMagick processes and we generally never have a performance issue. We describe how we set policies to ensure uninterrupted service @ https://imagemagick.org/script/security-policy.php.

Post Reply