3

So for example in my program I have a window called MainWindow. In this window I have a frame which I load a page into using the mousedown event on the navigation menu.

If I call that Page1, what happens if I decide to load another page, say Page2, into that frame? Does the old Page get held in memory? Or does it get deleted?

2 Answers 2

3

By default a page gets deleted, but setting the KeepAlive property to true will keep it in memory.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowTitle="WillBeKeptInMemory"
    KeepAlive="True"
    >
2
  • Except that the journal keeps pages alive. If you add a page to a frame, change text in a textbox then add another page. Then navigate back. The text you typed is still there.
    – Andy
    Feb 23, 2019 at 19:07
  • @Andy that's not true. See my comments to your answer, Feb 24, 2019 at 7:25
2

By default the Old page gets held in memory.

A Frame has a journal.

This is a pretty tricky aspect of Frames because it will keep a reference to pages.

Any page you add to a frame is kept alive so long as the journal hangs on to it.

From the docs, there is this rather odd bit in KeepAlive:

"true if the Page instance is retained in navigation history; otherwise, false. The default is false." Which really means the default is true. And the non default is kind of true, as it turns out.

If this is what you want then great.

If this is not what you want then you potentially have a memory hog.

Code demonstrating this:

    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="160"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel>
        <Button Name="ShowPage1"
                Content="Show Page One"
                Tag="{x:Type local:Page1}"
                Click="ShowPage_Click"
                />
        <Button Name="ShowPage2"
                Tag="{x:Type local:Page2}"
                Click="ShowPage_Click"
                Content="Show Page Two"/>
    </StackPanel>
    <Frame Grid.Column="1"
           Name="TheFrame"
           />
</Grid>

Code behind

    public MainWindow()
    {
        InitializeComponent();
    }

    private void ShowPage_Click(object sender, RoutedEventArgs e)
    {
        Button btn = sender as Button;
        Type pageType = btn.Tag as Type;
        var pg = Activator.CreateInstance( pageType);
        TheFrame.Content = pg;
    }

Page 1

  Title="Page1"

  Background="Yellow"
  >

<StackPanel>
    <TextBlock Text="This is Page 1"/>
    <TextBox />
</StackPanel>

Page 2

  Background="Pink"
  >
<StackPanel>
    <TextBlock Text="This is Page 2"/>
    <TextBox />
</StackPanel>

Steps. Click Show Page 1 and type in the box.

enter image description here

Click Show Page 2 and type anything in the box

enter image description here

Notice the navigation bar is there. Click back and you will see the original page 1. Which is in memory. Click forward again. Here we are back with Page 2 again. Click Show Page 1. Type in that enter image description here

Then click the navigate button to go back.

Guess what?

The first instance of Page 1 is still there.

In memory.

Alive.

Even though I didn't set KeepAlive at all.

Here's my solution: Frames Are Evil

If I set the KeepAlive to false then the data I entered disappears.

So that means the memory is freed up?

As it turns out no.

Not always.

You will still find there's memory usage.

Tricky stuff.

Unless you understand exactly how they work and you're good with that, Frames and pages are best avoided.

All substantial projects I've worked on have instead used Contentcontrol and UserControls templated from viewmodels using the viewmodel first pattern.

6
  • If you create the page instances yourself ans assign then to Page.Content, the navigation journal indeed has to keep references to your pages (because it has no way to reconstruct the page on demand). If you instead navigate via Frame.Navigate(Uri), the navigation journal only remembers the Uri, not the page instance. Feb 24, 2019 at 7:08
  • An additional remark: your test for determining whether or not the page was kept alive is not conclusive. The navigation journal remembers the current state of several control types and restores it even if the page is recreated from scratch. See MSDN Feb 24, 2019 at 7:24
  • And where is the state of those controls stored? In memory. It is all too easy to find unexpected behaviour. This is quite complicated, counter intuitive and very easily misunderstood. as evidenced by a recent thread where the OP had problems with pages consuming excessive memory. But why bother in the first place? Viewmodel first is a fairly straight forward mainstream approach.
    – Andy
    Feb 24, 2019 at 10:45
  • I agree that using the NavigationService if you do not want the Journal is rather pointless. Feb 24, 2019 at 10:51
  • I'm a bit confused but it seems they are held in some form in memory then, like a cache, in order for the user to navigate between Pages? But if I set my keep alive to false it should, in theory, at least clear the Page from memory even if it doesn't release the actual memory assigned to it?
    – Cypher236
    Feb 25, 2019 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.