microsoft-ui-xaml: Navigation Memory Leak
Describe the bug We have noticed that our UWP apps have memory leaks. I have investigated it and found out that when navigating to new pages, the memory gets higher and doesn’t seem to go down by much even when GC runs. The pages do get garbage collected (Their finalizer is run), but it seems like some kind of unmanaged memory is not properly cleaned up.
Steps to reproduce the bug
I have put together a small repro that consists of two pages:
- MainPage
<Page>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70" />
<RowDefinition />
</Grid.RowDefinitions>
<Button x:Name="navigateButton" Content="Navigate" HorizontalAlignment="Center"
Click="NavigateButton_Click" />
<Frame x:Name="mainFrame" IsNavigationStackEnabled="False"
Padding="10" Grid.Row="1" />
</Grid>
</Page>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void NavigateButton_Click(object sender, RoutedEventArgs e)
{
mainFrame.Navigate(typeof(Page1), null, new Windows.UI.Xaml.Media.Animation.DrillInNavigationTransitionInfo());
}
}
- Page1:
<Page>
<VariableSizedWrapGrid ItemWidth="100" ItemHeight="60">
<Button Padding="20, 10" Content="Hello!"/>
<Button Padding="20, 10" Content="Hello!"/>
<Button Padding="20, 10" Content="Hello!"/>
<Button Padding="20, 10" Content="Hello!"/>
<Button Padding="20, 10" Content="Hello!"/>
<!-- And 25 more buttons here -->
</VariableSizedWrapGrid>
</Page>
public sealed partial class Page1 : Page
{
public Page1()
{
this.InitializeComponent();
}
~Page1()
{
Debug.WriteLine("Page dead :(");
}
}
The full source code is available on GitHub.
I have also tried out setting Frame.IsNavigationStackEnabled to false, it doesn’t help.
What am I doing wrong here?
Expected behavior Memory is reclaimed after Garbage collector runs
Screenshots

And you can see a video of the repro.
Version Info NuGet package version: N/A (Not using WinUI)
| Windows 10 version | Saw the problem? |
|---|---|
| Insider Build (xxxxx) | |
| May 2019 Update (18362) | Yes |
| October 2018 Update (17763) | Yes |
| April 2018 Update (17134) | |
| Fall Creators Update (16299) | |
| Creators Update (15063) | |
| Anniversary Update (14393) |
| Device form factor | Saw the problem? |
|---|---|
| Desktop | Yes |
| Mobile | |
| Xbox | |
| Surface Hub | |
| IoT |
Additional context
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 25 (8 by maintainers)
@cheles Thank you for your work.I know it’s getting better. Many questions about UWP are usually unanswered, which is very disturbing. UWP Framework also have many memory issues.Hope to fix it as soon as possible.
Why doesn’t Microsoft test it and fix it?
In case anyone else needs it, I ended up writing a little helper class as a quick fix to this while we evaluate more direct options. I’m calling
CleanUp.FrameworkElementon our pages when they’re navigated away from (and not being cached), and it has significantly cut down our app’s constant memory growth on page navigation.Here’s a simplified version that should be fine for common use:
then in our shared PageBase class:
This is still an issue in WinUI3 using Microsoft.WindowsAppSDK version 1.5.240802000 (the latest version as of 2024-08-14). I’ve attached a sample reproduction against WinUI3 (please let me know if I should recreate this as a separate issue, since this one was originally for UWP).
The example has three parts:
SmallPage- a page with just a few basic controlsLargePage- a page with aListViewof 100 items, each of which has several controls in its data template. This is to replicate the number of controls on a data-heavy page of our line of business app we’ve written in WinUI3.MainWindow- the main window, which hosts a Frame that exhibits the problem, a textblock to show information about the frame and current page, and a button to triggerGC.Collect()I have set
IsNavigationStackEnabledtoFalsefor the frame, and both pages are using the defaultNavigationCacheModeofDisabled, so WinUI3/WinRT should not be attempting to cache any data.If we watch the memory usage in Visual Studio we can see that the memory jumps up by a few MB every time
SmallPagenavigates toLargePage, and does not go back down when LargePage is navigated away from or when GC.Collect is explicitly called. Visual Studio doesn’t show it well, but I’ve also profiled this same issue with dotMemory in our main application (unpackaged) and you can see that the memory that isn’t being released is native memory, not managed, which indicates it’s WinRT causing the leak.Please give us a status update on this, our application is expected to be run by our customers for most of their day without having to restart it, and they run in a shared environment. Large memory leaks like this are a problem.
For those curious, this is what our application does when it runs into this problem (app is opened, the data-heavy page is opened, the page is closed, the page is opened again, etc):
This problem also exists when using C++/WinRT. The destructor does get called when switching pages, but the memory continues to grow.
It’s disappointing that this bug has not been fixed after three years.
@cheles This problem has existed for a long time and has not been solved, even with the updated Microsoft.NETCore.UniversalWindowsPlatform to lastest. I’ve seen a lot of people mention things like this. such as https://github.com/dotnet/runtime/issues/12255. Look at the last few replies,from the snapshot,ConditionalWeakTable<T> leak memory. In other case,it is not necessary to switch pages. Frequent remove and add interface elements can also cause this problem. We don’t know what’s going on inside, but that’s how it shows.
@cheles I actually do! Thanks, that’s great to hear and I hope you continue these optimizations
This issue is also cross-posted to https://stackoverflow.com/questions/56675628/uwp-navigation-memory-leak. We can investigate as a platform bug and report back to stack overflow when we understand this.
@MikeHillberg can you debug this repro?