dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.05k stars 1.73k forks source link

[BUG] Memory Leak using SkiaBitmapExportContext #25079

Open dcaponetti opened 1 week ago

dcaponetti commented 1 week ago

Description

Good morning everyone, We use Microsoft.Maui.Graphics and Microsoft.Maui.Graphics.Skia (ver 8.0.91) to generate images containing text and black and white graphics.

Unfortunately a serious memory leak problem causes the pods running on Kubernetes to restart every 3/4 hours. I have included all the usings I could:

 using (SkiaBitmapExportContext page = new SkiaBitmapExportContext(

     (int)HelperGraphic.PixelFromMillimeters(Convert.ToSingle(p.Width, System.Globalization.CultureInfo.InvariantCulture), _dpi),
     (int)HelperGraphic.PixelFromMillimeters(Convert.ToSingle(p.Height, System.Globalization.CultureInfo.InvariantCulture), _dpi),
     1.0f,disposeBitmap:true))
 {
     ICanvas g = page.Canvas;

     g.FillRectangle(0, 0,
         HelperGraphic.PixelFromMillimeters(Convert.ToSingle(p.Width, System.Globalization.CultureInfo.InvariantCulture), _dpi),
         HelperGraphic.PixelFromMillimeters(Convert.ToSingle(p.Height, System.Globalization.CultureInfo.InvariantCulture), _dpi));
     g.FillColor = Colors.White;
[.....]
 using (IImage image = SkiaImage.FromStream(ms))
 {
[....]

But the problem is not solved. I attach the dump file of MemoryDump.zip the memory executed directly on the K8 pod.

Any ideas? Thanks a lot.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.0-rc.1.24453.9

Is this a regression from previous behavior?

No, this is something new, Not sure, did not test other versions

Last version that worked well

8.0.91 SR9.1

Affected platforms

Windows, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

No response

Did you find any workaround?

no

Relevant log output

No response

jfversluis commented 1 week ago

From the data in the form I'm seeing some mixed signals. You indicate this is a new potential issue, but also that it worked well in version 8.0.91 SR9.1? Is this something that is happening since a recent update to .NET 9 RC1? Can you please confirm?

Also is this happening on Windows or other platforms too? What makes you think this is a memory leak in .NET MAUI and not your own code?

Its a bit hard to diagnose with the little code that is provided. Would you be able to put together a reproduction project, that would be very helpful.

Thanks

dcaponetti commented 1 week ago

From the data in the form I'm seeing some mixed signals. You indicate this is a new potential issue, but also that it worked well in version 8.0.91 SR9.1? Is this something that is happening since a recent update to .NET 9 RC1? Can you please confirm?

Also is this happening on Windows or other platforms too? What makes you think this is a memory leak in .NET MAUI and not your own code?

Its a bit hard to diagnose with the little code that is provided. Would you be able to put together a reproduction project, that would be very helpful.

Thanks

Hi , thnaks a lot for you

From the data in the form I'm seeing some mixed signals. You indicate this is a new potential issue, but also that it worked well in version 8.0.91 SR9.1? Is this something that is happening since a recent update to .NET 9 RC1? Can you please confirm?

Also is this happening on Windows or other platforms too? What makes you think this is a memory leak in .NET MAUI and not your own code?

Its a bit hard to diagnose with the little code that is provided. Would you be able to put together a reproduction project, that would be very helpful.

Thanks

Thanks for your attention, it was my mistake to indicate that it worked with a previous version, in reality we noticed memory leak as soon as we migrated from System.drawings (no longer supported on linux) to Microstft.Maui.

The problem occurs only on Linux environments on Kubernetes cluster, on my development PC in Windows environments the memory is always correctly deallocated so I am quite sure that it does not derive from my code. In addition, analyzing the memory dump that I attached, you can see a significant amount of unmanaged memory that is never deallocated.

I thank you for any advice you can give me, if I can in the next few days I will try to create a simplified project that can reproduce the problem.

Daniel

dcaponetti commented 4 days ago

Hello, I created a test project to reproduce the problem. I noticed that the memory leak is generated only in web api content with controller and addScoped service. I also tried with a simple web application and with a console application and the problem does not occur. This is the link to the project: https://github.com/dcaponetti/TestImageAPI

Thanks for any possible support.

dcaponetti commented 2 days ago

Any update? @jfversluis @samhouts