Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when multi-threading on Linux #708

Open
tsvx opened this issue Jun 3, 2021 · 1 comment
Open

Memory leak when multi-threading on Linux #708

tsvx opened this issue Jun 3, 2021 · 1 comment

Comments

@tsvx
Copy link

tsvx commented Jun 3, 2021

This is a duplicate of the bug in the System.Drawing.Common since I do not know exactly what leads to the problem.

Description

Consider this following piece of code:

async Task DrawCollage()
{
    var jpegBytes = File.ReadAllBytes("image.jpg"); // any 2000*1500 px, 24 bpp
    using var collage = new Bitmap(1000, 1000);
    using var g = Graphics.FromImage(collage);
    //using var ms = new MemoryStream(jpegBytes);
    //using var image = Image.FromStream(ms);
    //g.DrawImage(image, 0, 0, 500, 500);
}

Note that every graphics object is disposed properly here.

Frequent serialized calls of this method on different threads lead to enormous memory consumption, even with three last lines commented out. Working Set size increases in proportion to the number of threads involved, but GC Heap does not increase.
Uncommenting every line triples the memory consumption.

When I force this method to use one dedicated thread only, no increase in memory consumption is observed.

I expect the memory should not grow as every graphic object is disposed after use.

Configuration

  • Ubuntu 18.04 x86_64
    • libgdiplus 4.4, 6.0.5
  • Ubuntu 18.04 arm64
    • libgdiplus 4.2-2
  • Debian 10 x86_64 (docker, mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim)
  • .NET Core 3.1
  • System.Drawing.Common 5.0.2

Regression?

The bug is not reproducible on Windows 10 x64 with .NET Core 3.1.

Other information

It seems something keeps being cached after dispose in the thread local storage in the libgdiplus library.

A complete code example to reproduce the problem is here: https://github.com/tsvx/SystemDrawingMemoryLeak
It is a work in progress (it reproduces the problem but can be simplified I think).

@sharpSteff
Copy link

I have the same issue on

  • Ubuntu 20.04 x64
  • .NET Core 3.1
  • libgdiplus 6.0.5, 6.0.4

On stackoverflow someone suggested installing libgdiplus 4.2. https://stackoverflow.com/questions/71478697/system-drawing-high-memory-usage-on-linux Unfortunately 20.04 has no 4.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants