2
votes

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.

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.

And you can see a video of the repro.

I have also tried out setting Frame.IsNavigationStackEnabled to false, it doesn't help.

What am I doing wrong here?

An image of memory use after clicking on the Navigate button for a while

1

1 Answers

1
votes

I am using Windows 1803 because I have not the latest 1809 but I think it works the same,

In the code of Page 1:

 protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        System.GC.Collect();
    }

It is another way to check if the App has to dispose the Pages. and it works.