Skip to content

Memory leak on Linux - Multi-threaded application #1854

@lkratochvil

Description

@lkratochvil

Magick.NET version

Magick.NET-Q8-x64 -> 14.6.0

Environment (Operating system, version and so on)

Linux - Ubuntu 24.04 in WSL or Linux in docker container (base image: mcr.microsoft.com/dotnet/sdk:9.0)

Description

Hi.

I think, there is memory leak when using Magick.NET on Linux during processing images. Allocated memory is never released and increases in time. Speed of increase depends on file size.

It seems to me similar to this issue #1163 where you somehow solved webp?

My use case:
I need to process single image or each frame of multi-framed image (like GIF for example), resize it using InterpolativeResize() and this resized Image I need to use for further processing. Also I have limited resources, so I need to setup limits.

To simplify this use case I created demo, where 5 threads are processing GIF or PNG. I've also implemented a mechanism where each iteration of image processing uses a different thread, as it appears each thread maintains its own reserved memory pool. The memory leak seems to occur because when a previously idle thread begins processing images, it allocates new resources that are never properly released.

Additional information
On windows, the memory management looks as expected, problem is just on Linux, where memory is never released. I was trying to play with GLIBC_TUNABLES as it was mentioned in this comment but without any success.

Expected Behavior:
Memory usage should stabilize after initial allocation, similar to Windows behavior.

Actual Behavior:
Memory usage continuously increases on Linux, leading to eventual out-of-memory conditions.

Thank you for your help.

Steps to Reproduce

Demo project:

ImageMagickMemoryLeak.zip

  • In Resources folder, there should be PNG1.png, PNG2.png, ..., PNG5.png and GIF1.gif, ... GIF5.gif. I could not add them into zip with project so please add them manually into Resources folder.

  • There is Dockerfile -> build image using: docker build -t image-magick-memory-leak .

  • Run image with PNG or GIF CMD : docker run -v /path/to/volume/:/tmp image-magick-memory-leak PNG or docker run -v /path/to/volume/:/tmp image-magick-memory-leak GIF

  • At start you have some time to go into container and start dotmemory profiling if you want to -> docker exec -it containerId /bin/bash -> cd /tmp -> /dotMemoryclt/tools/dotmemory attach 1

  • or you can just look at attached dmw files or pictures from the process done by myself

Windows - expected behavior:

windows_dmws.zip
PNG:

Image
GIF:

Image

Linux - increasing in time:

linux_dmws.zip
PNG:

Image
GIF:

Image

Images

used_images.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions