Peter
Peter

Reputation: 43

Navigation of pages and running in the background

In UWP (XAML / C#) I use Frame.Navigate(typeof(Page2));, and in C# of Page2 I use timer and when I use Frame.GoBack();, the frame really goes back, but the timer is not stopped - I mean the page and all its components are still running in the background, and the app is consuming too much RAM because of that. How can I "kill" the page?

note: if user uses this navigation 10 times, the page is 10 times in the background and it is bad..

Upvotes: 0

Views: 519

Answers (1)

Yarik
Yarik

Reputation: 1578

It is important to understand that CLR garbage collector is the one who is responsible for "killing" unused objects. An object (and all of it's members) becomes "unused" when it is no longer referenced.

When you start a Windows.UI.Xaml.DispatcherTimer, it adds itself to a timer collection inside the current Dispatcher, thus, creating a direct reference between the Dispatcher and the timer. The timer, in its turn, holds a reference to the page where it is running. Since the Dispatcher is a global object, it will keep your page alive until the timer is stopped.

There can be other causes of memory leak (it is a pretty broad topic), including:

  • Other sources of direct or indirect references to your page;
  • Subscriptions to static events;
  • Complex data bindings like {Binding Path=Property.Subproperty};

I would suggest you to use a memory profiler to find memory leaks if the above doesn't help, such as the Diagnostic Tools included in Visual Studio 2015.

Upvotes: 1

Related Questions