Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP Windows 10 App memory increasing on navigation

I have a UWP Windows 10 App and noticed the memory usage in task manager is increasing over time.

I stripped the App back and found the memory is increasing when the navigating pages. So I made a simple app with just a few pages to test and the memory is still increasing in this simple App. I have a MainPage that navigates a frame from Page1 to Page2 and back on a timer.

    public sealed partial class MainPage : Page
{

    private DispatcherTimer _timer;

    private bool _page1Showing;
    private bool _timerRunning;

    public MainPage()
    {
        this.InitializeComponent();

        _timer = new DispatcherTimer();
        _timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
        _timer.Tick += _timer_Tick;
    }

    private void _timer_Tick(object sender, object e)
    {
        GC.Collect();

        this.rootFrame.BackStack.Clear();
        this.rootFrame.ForwardStack.Clear();

        if (_page1Showing)
        {
            this.rootFrame.Navigate(typeof(Page2));
            _page1Showing = false;
        }
        else
        {
            this.rootFrame.Navigate(typeof(Page1));
            _page1Showing = true;
        }
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (_timerRunning)
        {
            _timer.Stop();
            _timerRunning = false;
        }
        else
        {
            _timer.Start();
            _timerRunning = true;
        }
    }

}

Page1 and Page2 are empty pages with a grid with a background color so you can see the navigation. While this App runs the memory usage in task manager increases by around 1MB every 30 mins.

I have run the App using the memory diagnostics in VS2015 the Managed heap is as expected:

enter image description here

The heap is always increasing:

enter image description here

Comparing snapshots of the Heap shows:

enter image description here

I am confused what these McgInterop objects are? and why such a simple App is always increasing in memory usage. My main App needs to run for a long time (months+). Appreciate any help.

I have tried changing the pages NavigationCacheMode, if set to Required the pages are created once. If set to disabled the Pages are created every time, and I checked the finalizer is called as expected.

--

Edit: I added a button to start and stop the timer (updated the above). It seems that while the timer is running the memory usage in Task manager will increase, when the timer is stopped the memory usage eventually drops.

I measured the memory usage in task manager over a day starting and stopping the timer around every 2 hours as follows, it slowly increases and then drops at some point:

12.5 -> 17.1 -> 16.7 -> 13.9 -> 16.8 -> 22.5 -> 13.6 -> 14.6 -> 24.9 -> 15.2

So I guess everything is working fine? But I am not clear what is happening here, why is it increasing so much? When is it being free'd under what conditions?

Is the system delaying releasing memory while pages are navigating? when the user would normally be interacting with the screen?

like image 606
tagy22 Avatar asked Nov 26 '15 11:11

tagy22


People also ask

How do I reduce memory usage in WPF?

Try this: Open the task manager, go to the processes tab, go to view, select collums and check the box that says VM size. Notice that when you minimize or restore a program or application the memory changes from what's in Memory Usage to VM size.


2 Answers

Every time you navigate to a Page, you create a new instance of Page, but the previous Page is not disposed (even if the Page is already in the navigation stack).

To prevent multiple allocation of same page, set NavigationCacheMode="Enabled" attribute to the Page.

Also, to minimize the memory allocation, you must override method OnNavigatedTo and OnNavigatedFrom.

In OnNavigatedTo method:

  • Instantiate all memory intensive object or resources
  • Add all events handlers you need
  • starts all timers, tasks or threads

In OnNavigatedFrom:

  • Dispose all resources
  • Stops all timers, tasks or threads
  • Remove references from all heavy objects
  • Remove all events handlers
  • Only if you really need, call GC.Collect()
like image 83
Stefano Balzarotti Avatar answered Sep 30 '22 08:09

Stefano Balzarotti


Can we see your xaml code? Are you using x:name in your xaml and is that being destroyed? If so that might cause your memory leak.

Look at this link if you are using x:name: http://support.scichart.com/index.php?/News/NewsItem/View/21/wpf-xname-memory-leak--how-to-clear-memory-in-scichart

Of course a UWP may handle x:name differently...

like image 35
ashlar64 Avatar answered Oct 03 '22 08:10

ashlar64