简体   繁体   English

C#UWP新视图内存泄漏

[英]C# UWP new view memory leak

With this code I am creating new view: 通过此代码,我可以创建新视图:

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

When I close that new window, and run code again, and again I close that new one and again I open another window, memory usage in windows is getting larger and larger...so, I think all the windows are not closed, but just hidden actually... 当我关闭该新窗口并再次运行代码时,我再次关闭该新窗口,然后再次打开另一个窗口,窗口中的内存使用量越来越大...所以,我认为所有的窗口都没有关闭,但是只是隐藏着...

So, there are 2 ways I am trying: 因此,我尝试了两种方法:

  1. Try to actually "kill" the closed window 尝试实际“杀死”关闭的窗口
  2. Try to re-show hidden window and make it's content different... 尝试重新显示隐藏的窗口并使其内容不同...

I just want there to be only 2 windows, main and this newly created... Any suggestion on how to do this? 我只希望只有2个窗口,主要窗口和这个新创建的窗口。关于如何执行此操作的任何建议?

Edit: 编辑:

This is what it looks like when I use this example https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews 这就是我使用此示例时的样子https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews

These are 3 screenshots with this app. 这是此应用的3个屏幕截图。

This is memory consumption when app is started: 这是启动应用程序时的内存消耗:

This is after I add 4 views: 这是在我添加4个视图之后:

And this is when I open and close each one of them: 这是当我打开和关闭其中的每一个时:

So, even in their example memory usage is rising... 因此,即使在他们的示例中,内存使用率也在上升……

When you close a window it is kept in the recently used windows list in case it is needed again. 关闭窗口时,该窗口将保留在最近使用的窗口列表中,以防再次需要。 If you know you won't need it again (or want/need to reclaim the memory) you can close it by calling Window.Current.Close(); 如果您知道不再需要它(或想要/需要回收内存),则可以通过调用Window.Current.Close();来关闭它Window.Current.Close(); after the ApplicationView.Consolidated event is called on that view. 在该视图上调用ApplicationView.Consolidated事件之后。

There's description of this in this article on the "buildingapps" blog . 本文在“ buildingapps”博客对此进行了描述。
There's also a (quite complicated IMO) example in the UWP samples repository . UWP样本存储库中还有一个(相当复杂的IMO) 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM