Skip to content
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

Memory leak in WinUI3 multiple window applications #9063

Open
Ajith-GS opened this issue Nov 9, 2023 · 12 comments
Open

Memory leak in WinUI3 multiple window applications #9063

Ajith-GS opened this issue Nov 9, 2023 · 12 comments
Labels
bug Something isn't working team-Reach Issue for the Reach team

Comments

@Ajith-GS
Copy link

Ajith-GS commented Nov 9, 2023

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();
        }
    }
  1. Build and run the application in debug mode.
  2. Click the button in the MainWindow
  3. Observe the memory usage in Visual Studio Diagnostic Tools.
  4. Dismiss all the windows except the MainWindow.
  5. 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.

@bpulliam bpulliam transferred this issue from microsoft/WindowsAppSDK Nov 9, 2023
@bpulliam bpulliam added the team-Reach Issue for the Reach team label Nov 9, 2023
@NicholasChrzan
Copy link

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.

#7282

@duncanmacmichael duncanmacmichael added the bug Something isn't working label Nov 9, 2023
@Ajith-GS
Copy link
Author

@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
Copy link

HO-COOH commented Mar 27, 2024

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
Copy link

leohu1 commented Apr 5, 2024

Can this be fix?

@leohu1
Copy link

leohu1 commented Apr 5, 2024

Even explorer.exe have this problem

@AzAgarampur
Copy link

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
Copy link

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

@lfmouradasilva
Copy link

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

@huguojunsy
Copy link

huguojunsy commented Aug 11, 2024

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
Copy link

HO-COOH commented Aug 13, 2024

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.

@jingliancui
Copy link

Hi @marcelwgn , do you or any teammates have any idea on this issue please? thanks.

@lhak
Copy link

lhak commented Nov 4, 2024

I tested the project in the initial post and with updated packages, I could only observe an increase of less than 1Mb if the 50 windows are opened and closed repeatedly. However, it seems that Visual Studio shows an inflated memory usage if xaml hot reload is enabled.

I also put some more observations here #9960 that might lead to additional memory leaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-Reach Issue for the Reach team
Projects
None yet
Development

No branches or pull requests