Memory leaks when using System.Windows.Forms
.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.
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
- Build the csproj in the above attachment.
- Execute the output exe and click on the button.
- Using Task Manager, monitor Private Bytes of the application every 3 mins for around 30 mins.
- There will be a steady increase in the Private Bytes of the application
The application is doing the following:
- We have
Form2that has checkbox, combobox and textbox controls (all from System.Windows.Forms namespace). -
Form2has hooked onto Load event in which it creates a Timer which is configured to call a function in one sec. - Function which Timer calls first stops the Time, Disposes it and closes the Form.
- We have
Form1that has a button. - Clicking on the button does the following in an infinite loop
- Creates instance of
Form2, calls ShowDialog and disposes the instance when it is done. - Calls GC.Collect and GC.WaitForPendingFinalizer
- Creates instance of
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.
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.
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
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.