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

Navigation Memory Leak

Open mhmd-azeez opened this issue 5 years ago • 25 comments

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:

  1. 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());
        }
    }
  1. 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 An image of memory use after clicking on the Navigate button for a while

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

mhmd-azeez avatar Jun 23 '19 17:06 mhmd-azeez

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?

jevansaks avatar Jun 24 '19 18:06 jevansaks

I also have this problem with the same settings cited.

r7dev avatar Jul 11 '19 20:07 r7dev

@Opiumtm explain it here, but it doesn't demonstrate the code.

r7dev avatar Jul 11 '19 21:07 r7dev

I leave my comment here, although it's an old post, for others that might search for it.

We made huge improvements in memory handeling and GC calls with latest UWP 6.2.9 Nuget update while targeting >=RS4 . The complete release notes are here: https://github.com/microsoft/dotnet/blob/master/releases/UWP/net-native2.2/README.md

cheles avatar Sep 20 '19 10:09 cheles

@cheles I am not using WinUI currently, is there a way for me to use these latest changes?

mhmd-azeez avatar Sep 20 '19 17:09 mhmd-azeez

@encrypt0r Microsoft.NETCore.UniversalWindowsPlatform is the default package for any UWP application. aren't you using this package?

cheles avatar Sep 23 '19 09:09 cheles

@cheles I actually do! Thanks, that's great to hear and I hope you continue these optimizations

mhmd-azeez avatar Sep 23 '19 10:09 mhmd-azeez

cool. test it and let us know if we can close this one out.

cheles avatar Sep 23 '19 10:09 cheles

@cheles unfortunately, it doesn't seem to help with the repro. I have disabled NavigationStack on the frame and yet, no memory is reclaimed after 3 garbage collections.

image

mhmd-azeez avatar Sep 23 '19 11:09 mhmd-azeez

Hi ! Any news about this problem please ?

florian-alexandre avatar Jan 21 '20 00:01 florian-alexandre

@encrypt0r are you debugging a Debug build or Release? GC is implemented differently and, in general, it's an expensive. In Release you will see GC being called more rarely than Debug.

93 MB out of, probably 3/4GB or RAM is not dangerously enough to cause GC to clean gen 0/1/2 objects. I've tried your sample in Debug and I've barely got to 80 MB after 80+ clicks. Try and see if you manage to reach at least 100MB+ or create an UI test with [WinAppDriver ] (https://github.com/microsoft/WinAppDriver/tree/master/Tests/UWPControls) to reach a higher memory consumption and observe GC calls

cheles avatar Jan 22 '20 13:01 cheles

Along the lines of what @cheles mentioned, I'd recommend you give this article a read. In particular item 4 of "What Causes Memory Leaks?".

kmgallahan avatar Jan 22 '20 15:01 kmgallahan

Also, if you want a more in-depth way to inspect memory consumption & GC calls, use Performance Monitor (Win10 built-in) https://devblogs.microsoft.com/dotnet/gc-performance-counters/

cheles avatar Jan 22 '20 15:01 cheles

@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.

CodeForCSharp avatar May 22 '20 16:05 CodeForCSharp

@CodeForCSharp I haven't said that UWP team fixed all memory issues in UWP. I just said that UWP 6.2.9 brought some memory consumption improvements per this link

Adjust garbage collection timing during interop to eliminate memory increase in UWP app when navigating between pages

https://github.com/dotnet/runtime/issues/12255 seems to have been marked for Future release but I don't see any due date to it.

cheles avatar May 25 '20 09:05 cheles

@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.

CodeForCSharp avatar May 25 '20 09:05 CodeForCSharp

@cheles, I am using latest version 6.2.10, and still facing this memory leak issue, which leads to app memory to 1 GB. Is there any fixed date for the resolution of this issue?

abk90007 avatar Jun 28 '20 05:06 abk90007

Why doesn't Microsoft test it and fix it?

cxllxn avatar Feb 22 '22 08:02 cxllxn

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.

Blinue avatar Jul 29 '22 02:07 Blinue

I have the same problem. When I press the ‘X’ to close the app, the last Page in Frame was not released, the destructor of last Page not called.

JoverQian avatar Feb 12 '23 03:02 JoverQian

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Aug 13 '23 20:08 github-actions[bot]