microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Memory leak in WinUI3 multiple window applications

Open Ajith-GS opened this issue 1 year ago • 10 comments

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.

Ajith-GS avatar Nov 09 '23 13:11 Ajith-GS