【问题标题】:C# UWP new view memory leakC# UWP 新视图内存泄漏
【发布时间】:2017-03-15 10:02:03
【问题描述】:

使用此代码我正在创建新视图:

CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        Frame frame = new Frame();
        frame.Navigate(typeof(SecondaryPage), null);
        Window.Current.Content = frame;
        // You have to activate the window in order to show it later.
        Window.Current.Activate();

        newViewId = ApplicationView.GetForCurrentView().Id;
    });
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);

当我关闭那个新窗口并再次运行代码时,我再次关闭那个新窗口并再次打开另一个窗口,窗口中的内存使用量越来越大......所以,我认为所有窗口都不是关闭了,但实际上只是隐藏了......

所以,我尝试了两种方法:

  1. 尝试真正“杀死”关闭的窗口
  2. 尝试重新显示隐藏窗口并使其内容不同...

我只希望只有 2 个窗口,主窗口和这个新创建的窗口... 有关如何执行此操作的任何建议?

编辑:

这就是我使用这个例子时的样子https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews

这是这个应用程序的 3 个屏幕截图。

这是应用启动时的内存消耗:

这是在我添加 4 个视图之后:

这是我打开和关闭它们的时候:

因此,即使在他们的示例中,内存使用量也在上升...

【问题讨论】:

    标签: c# memory-leaks uwp forceclose multiple-views


    【解决方案1】:

    当您关闭一个窗口时,它会保留在最近使用的窗口列表中,以防再次需要它。如果您知道不再需要它(或想要/需要回收内存),您可以在该视图上调用 ApplicationView.Consolidated 事件后通过调用 Window.Current.Close(); 来关闭它。

    this article on the "buildingapps" blog 中有对此的描述。
    还有一个(相当复杂的 IMO)example in the UWP samples repository

    【讨论】:

    • 不过,无论我做什么,每次我关闭和打开视图时内存使用量都会上升......
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2021-01-14
    • 2011-12-24
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多