UWP Windows 10 App memory increasing on navigation

后端 未结 3 1062
無奈伤痛
無奈伤痛 2020-12-25 11:09

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

3条回答
  •  既然无缘
    2020-12-25 12:06

    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()

提交回复
热议问题