dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.41k stars 982 forks source link

Memory leaks when using System.Windows.Forms #12096

Closed shashidhar-ks closed 1 week ago

shashidhar-ks commented 2 months ago

.NET version

.NET 8 (8.0.8)

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

Private bytes of the .NET 8 Windows Form application keeps increasing, when we open and close a Form.

The steps to reproduce section has details on how to reproduce it faster in a simpler way.

I used memprofiler to confirm that there are no leaks in the source code I have used. Memprofiler mentioned that there are increase in the types of object as mentioned in the below image.

image

Note: We are calling GC.Collect() and GC.WaitForPendingFinalizer() at regular interval and still the private bytes of the application keeps increasing.

Steps to reproduce

WinFormsApp1.zip

The application is doing the following:

So, clicking on the button will continuously creates and closes new Window Form. By monitoring Private Bytes of the application, we can see that memory of the application keeps increasing.

JeremyKuhne commented 2 months ago

It looks like you have an accessibility tool running (as you're getting ControlAccessibleObjects). Trying this with both .NET 8 and 9 with and without Narrator running I didn't see anything particularly concerning.

.NET 9 RC1 will be out shortly and would be worth comparing. The accessibility interop code has been completely rewritten and may impact what you're observing.

shashidhar-ks commented 1 month ago

Sorry for delay in reply. The main issue I want to bring to notice is that, the private byte memory of the application increase, which never goes down. Since it never goes down, calling it as memory leak.

Following contains data of private memory, when we execute the application mentioned in the description.

Private bytes when applications starts -> 7864K Private bytes when applications starts + 10 mins -> 13272K Private bytes when applications starts + 20 mins -> 19832K Private bytes when applications starts + 45 mins -> 37568K Private bytes when applications starts + 65 mins -> 51744K

merriemcgaw commented 1 week ago

We can't really move forward with this without more data. We aren't seeing this issue with the repro application. If we can get more information to proceed we'd be happy to take a look further, but there isn't enough information here to proceed further.