Reputation: 2370
I have a UWP kiosk app that is leaking memory. When I view it running in Task Manager I can see the memory consumption increasing as the app's pages are navigated. This navigation is done mostly without human intervention and is initiated via a seperate system responding to events in the environment, like a car driving past a sensor.
At the conclusion of the events the app navigates itself back to the starting page. I can see that as it navigates the backstack increases and never resets so I assume (and from reading other so questions such as this one) that this is the cause of the leak. I never call Frame.GoBack();
I use this simple method to navigate through the app:
public async Task NavigateTo(Type kioskPage)
{
try
{
if (Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
{
Frame.Navigate(kioskPage, KioskModel);
}
else
{
await Task.Run(() => Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Frame.Navigate(kioskPage, KioskModel);
}));
}
}
catch (Exception ex)
{
//maybe a race condition
Task.Delay(500).Wait();
try
{
await Task.Run(() => Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Frame.Navigate(kioskPage, KioskModel);
}));
}
catch (Exception ex2)
{
HandleError(ex2, TrackedEventSources.Code, false, "Double thread exception", true, "NavigateTo");
}
}
}
So if I want to navigate back to the starting screen I just call:
await NavigateTo(typeof(MainView), aModel);
This works whether on the main thread or a background one.
Is there a standard way in which an application should navigate home so that the backstack is reset to 0? Or is there something I'm doing wrong in terms of how I'm navigating?
I can call:
Frame.BackStatck.Clear();
But this doesn't free up the memory.
EDIT: I can also do this but it doesn't free up the memory (which may mean it's not the frames that are the problem):
public void NavigateToHome()
{
if (Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
{
while (Frame.CanGoBack)
{
Frame.GoBack();
}
}
else
{
Task.Run(() => Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
while (Frame.CanGoBack)
{
Frame.GoBack();
}
}));
}
}
Upvotes: 0
Views: 70
Reputation: 2370
It ended up being the pages were being held in memory through each navigation. Instead of navigating forwards all the time I need to go back when returning to the start page. Like so;
Frame = Window.Current.Content as Frame;
while(!(Frame.SourcePageType.Name == "HomePage"){
Frame.GoBack();
}
Upvotes: 0