microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Memory leak in WinUI3 multiple window applications #9063

Open Ajith-GS opened 10 months ago

Ajith-GS commented 10 months ago

Describe the bug

The memory is not released after dismissing WinUI 3 windows. Consider a WinUI3 app that creates multiple XAML windows on a button click. Each time a window is created, the memory usage increases. But memory usage is not decreasing when dismissing the windows.

Steps to reproduce the bug

  1. Create a C++ WinUI3 project using "Blank App, Packaged (WinUI 3 in Desktop)" template.
  2. Make it as unpackaged application using the steps described in the following link. https://learn.microsoft.com/en-us/windows/apps/winui/winui3/create-your-first-winui3-app#unpackaged-create-a-new-project-for-an-unpackaged-c-or-c-winui-3-desktop-app
  3. Add a new XAML window by right clicking the project -> Add -> New Item... -> Select Blank Window(WinUI3 in Desktop) template -> give name (for E.g.: SubWindow) -> Add.
  4. From the button click handler of the MainWindow create multiple instances of SubWindow and also activate them as shown in below.
    void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        for(int nIdx = 0; nIdx < 50; nIdx++)
        {
            auto subWin = make<SubWindow>();
            subWin.Activate();
        }
    }
  5. Build and run the application in debug mode.
  6. Click the button in the MainWindow
  7. Observe the memory usage in Visual Studio Diagnostic Tools.
  8. Dismiss all the windows except the MainWindow.
  9. Observe the memory usage in Visual Studio Diagnostic Tools.

Expected behavior

When each window is closed, the memory use should be reduced.

Screenshots

image001

No response

NuGet package version

Windows App SDK 1.4.2: 1.4.231008000

Packaging type

Unpackaged

Windows version

Windows 10 version 22H2 (19045, 2022 Update)

IDE

Visual Studio 2022

Additional context

A sample application with above described issue is also attached. MemoryTest.zip How do we fix this issue? Is there any workaround available to fix this issue? Any help would be highly appreciated.

NicholasChrzan commented 10 months ago

A workaround is to not use x:bind in your bindings and to manually unwire all event handlers even those done in XAML. This will allow the window to be properly garbage collected.

https://github.com/microsoft/microsoft-ui-xaml/issues/7282

Ajith-GS commented 10 months ago

@NicholasChrzan Thank you for the reply, The given sample application doesn't have any XAML design or x:bind and is still not releasing memory.

HO-COOH commented 5 months ago

This should got more attention. It's still leaking in Winui3 1.5. Dismissing the window does not release a single bit of memory. Can reproduce simply with the official WinUI 3 Gallery app in the multiple windows page.

leohu1 commented 5 months ago

Can this be fix?

leohu1 commented 5 months ago

Even explorer.exe have this problem

AzAgarampur commented 4 months ago

Explorer is very bad, memory usage grows to around a gigabyte with time and a bunch of threads msctf.dll!ShellHandwriting::DelegationManager::DelegateThreadProc are consuming memory too

rodirigos commented 2 months ago

Still happening to this day in dotnet8, even in empty windows.

lfmouradasilva commented 2 months ago

I have the same problem in my project that has already been launched in production. Does anyone have a workaround?

huguojunsy commented 1 month ago

You can reproduce this by switch between two tabs in WinUI3 Gallery app.But there is no such problem in winui2 Gallery . Maui may also be affected by this bug.

HO-COOH commented 1 month ago

You can reproduce this by switch between two tabs in WinUI3 Gallery app.But there is no such problem in winui2 Gallery . Maui may also be affected by this bug.

UWP is also affected by x:Bind and will have leaks.