-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
System.Drawing.Graphics memory leak when multi-threading on Linux #53663
Comments
Tagging subscribers to this area: @safern, @tarekgh Issue DetailsDescriptionConsider this following piece of code (drawing a collage): 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);
} 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. When I force this method to use one dedicated thread only, no increase in memory consumption is observed. Seems like something is cached in the thread local storage in the libgdiplus library. Configuration
Regression?The bug is not reproducible on Windows 10 x64 with .NET Core 3.1. Other information
|
Hi @tsvx |
We faced the same problem. In our case, it leads to denial of service, since there are limits on memory consumption in k8s. |
@tsvx thank you for raising the issue. I did look at this and it seems like it is libgdiplus specific, there is a lot of memory that they are missing to free when disposing a graphics object.
Agreed, and we have stated in our documentation that we don't recommend I'm going to close this issue as it is Please read this document for more information about this decision and let us know if you have any questions: https://github.com/dotnet/designs/blob/main/accepted/2021/system-drawing-win-only/system-drawing-win-only.md |
This is a duplicate of the bug in the libgdiplus since I do not know exactly what leads to the problem.
Description
Consider this following piece of code:
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
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.
Note that the memory does not grow (no visible growth) if the considered task runs infrequently.
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).
The text was updated successfully, but these errors were encountered: