locked
Navigating to the same xaml-page numerous times - bad practice? RRS feed

  • Question

  • My game navigates to a new instance of Playfield.xaml after each level. When I startet programming the game a year ago, I thought this would be a good idea since I don't have to cope with resetting the playfield after the player finishes the level. I automatically start with a clean new playfield. Now I'm not sure it that was a good idea.

    If the player moves trough let's say 50 levels, meaning Playfield.xaml gets instanciated 50 times, including all it's objects and subclasses it creates - is that a potential problem? Or do the "old" pages get garbage collected automatically anyway? Is there something I can or need to do to ensure this old stuff get's garbage collected?

    Rewriting the code to "reuse" the single instance of Playfield.xaml shouldn't be too much of a problem. But I wouldn't do it if there's no need to. Maybe my current solution is not that bad as I fear it may be. Input anyone? Many thanks in advance!

    Friday, January 31, 2014 11:14 AM

Answers

  • Once a page goes out of scope, it will get garbage collected. That does not mean that all of the objects within that page are GCed - it depends on how long they stay in scope.  Most of the time this isn't an issue.  I wouldn't worry about this unless you notice a steady increase in memory pressure as the app is used for that period of time.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Raymond AT Sunday, February 2, 2014 5:02 PM
    Friday, January 31, 2014 1:31 PM
    Moderator

All replies

  • Once a page goes out of scope, it will get garbage collected. That does not mean that all of the objects within that page are GCed - it depends on how long they stay in scope.  Most of the time this isn't an issue.  I wouldn't worry about this unless you notice a steady increase in memory pressure as the app is used for that period of time.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Raymond AT Sunday, February 2, 2014 5:02 PM
    Friday, January 31, 2014 1:31 PM
    Moderator
  • Thanks Matt. I did some tests and I think I'm doing fine. I hope :-) Thanks again.
    Sunday, February 2, 2014 5:01 PM