question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when using MagickImage constructor with Stream argument for large WebP image.

See original GitHub issue

Magick.NET version

10.1.0

Environment (Operating system, version and so on)

Windows 11

Description

Hi. I think I’ve found a memory leak specifically in the MagickImage constructor that takes a Stream argument.

When loading a 6MB animated WebP image using the MagickImage class, I’ve noticed that the large memory usage used during the loading does not clear itself after the Using block. This only happens when using the constructor that takes a Stream argument, the other constructors seem to work fine. (I understand that Magick.NET loads in the image as uncompressed pixel data, so the initial memory usage is high, but it clears after the object is disposed normally.) Shown below I have two sets of code to demonstrate the problem.

I can provide a large animated WebP file if you need, but I will have to edit it to take out our customer’s specific information first.

Steps to Reproduce

This code shows that the memory after loading the image is released correctly when using the file path constructor. If you look at the memory usage in the Task Manager once it is at the ReadLine() line, it will be low.

using System;
using ImageMagick;

namespace WebPMemLeak
{
    class Program
    {
        static void Main(string[] args)
        {
            const string IMAGE_PATH = "test.webp";

            using (MagickImage image = new MagickImage(IMAGE_PATH))
            {
                Console.WriteLine("Image loaded");
            }
            Console.WriteLine("Note the memory usage in the Task Manager, it is low.");
            Console.ReadLine();
        }
    }
}

This code shows that the memory after loading the image is still high when using the Stream constructor. If you look at the memory usage in the Task Manager once it is at the ReadLine() line, it will be high. Note that the file stream is closed properly with a Using block.

using System;
using ImageMagick;

namespace WebPMemLeak
{
    class Program
    {
        static void Main(string[] args)
        {
            const string IMAGE_PATH = "test.webp";

            using (System.IO.FileStream fs = new System.IO.FileStream(IMAGE_PATH, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                using (MagickImage image = new MagickImage(fs))
                {
                    Console.WriteLine("Image loaded");
                }
            }
            Console.WriteLine("Note the memory usage in the Task Manager, it is high.");
            Console.ReadLine();
        }
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dlemstracommented, Mar 17, 2022

Found and fixed the issue. This will be resolved in the next release of Magick.NET.

0reactions
dlemstracommented, Mar 17, 2022

I can reproduce your issue. Thanks for reporting this. Will try to figure out what is causing this tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WEBP: Coder enhancements/memory leak fix
I just started working with WEBP and noticed that the coder can't do WEBP lossless, and it also has a memory leak.
Read more >
GraphicsMagick News
Use GetFirstImageInList() when the pointer to the first image in the list is needed. ... Also, address memory leaks which may occur if...
Read more >
Diff - 2900797^! - platform/external/ImageMagick
The default is to use the C++ memory allocator. ... may be used to provide support for a specific named format (provided as...
Read more >
libMagick++-devel-32bit-7.1.0.9-150400.6.18.1 RPM for x86_64
This is Magick++, the object-oriented C++ API for the ImageMagick image-processing library. Magick++ supports an object model inspired by PerlMagick.
Read more >
Memory leaks - CVE - Search Results
A vulnerability in the Link Layer Discovery Protocol (LLDP) feature for Cisco Nexus 9000 Series Fabric Switches in Application Centric Infrastructure (ACI) Mode ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found