我有一个大项目,我的应用程序一直保留我导航离开的页面.该页面仅使用最少,并且有很多图形,因此我希望将其从内存中完全删除.
I have a big project where my application keeps retaining a page which I navigated away from. The page is only used minimal, and have a lot of graphics, I therefore want it to be completely removed from memory.
因此我使用了以下
NavigationService.RemoveBackEntry();
使用我看到的分析器,上面的代码片段确保我只有 1 个页面实例.但由于它的图形很重,我仍然希望它完全从内存中删除,即分析器中没有实例.
Using the profiler I saw that, the above snippet made sure that I would only have 1 instance of the page. But as it is heavy with graphics I still want it to be completely removed from memory, i.e. no instances in the profiler.
在我的大型应用程序中,我尝试取消订阅所有事件,引入 dispose/finalize 和调用 GC,它有所帮助,但实例仍然存在.
In my big application I tried to unsubscribe to all events, introduce dispose/finalize and calling GC, it helped some but the instance still existed.
为了排除任何愚蠢的错误,我制作了 这个小样本.仅使用内存弹出检查器在两个哑页面之间导航.但是仍然存在 1-2 个页面实例.是否有强制删除页面以使其没有任何内容存储在内存中的方法?
To exclude any stupid errors, I have made this small sample. Only Navigating between two dumb pages with a memory popup checker. But still 1-2 instances of the pages still exists. Is there anyway to force the removal of pages such that nothing of it is stored in memory?
我已添加:
while (App.RootFrame.RemoveBackEntry() != null) ;
到 OnNavigated to,它会删除除我开始的第一页之外的所有页面.我使用了调试分析工具包,可以看到无论我从哪个页面开始,当我离开它时,它都不会被删除.
To the OnNavigated to, and it removes all the pages except the first page I start on. I've used the debug analysis toolkit, and can see that no matter what the first page I start on does not get removed, when I navigate away from it.
WP Silverlight 运行时将在内存中最多保留三页,即使从后台堆栈中删除也是如此.我仍然不清楚这种行为的原因,但我找到了一个(丑陋的)解决方法:http://blogs.codes-sources.com/kookiz/archive/2013/11/11/wpdev-give-that-memory-back.aspx
The WP Silverlight runtime will keep up to three pages in memory, even after being removed from the backstack. The reason for this behavior is still unclear to me, but I've found a (ugly) workaround: http://blogs.codes-sources.com/kookiz/archive/2013/11/11/wpdev-give-that-memory-back.aspx
基本上,覆盖页面的 OnNavigatedTo
处理程序,并强制垃圾回收 3 次,通过调用调度程序分开:
Basically, override the OnNavigatedTo
handler of your page, and force a garbage collection three times, separated by calls to the dispatcher:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.Dispatcher.BeginInvoke(() =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
this.Dispatcher.BeginInvoke(() =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
this.Dispatcher.BeginInvoke(() =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
});
});
});
}
听起来很疯狂,但它确实有效.
As crazy as it sounds, it works.
在您的情况下,您还有另一个问题.您通过弹出窗口使页面保持活力.让我解释一下:
In your case, you have another problem. You are keeping the page alive with your popup. Let me explain:
在 CreatePopups
方法中,您创建弹出窗口并将其添加到起始页的网格中.在弹出窗口中,您启动一个计时器以定期调用 UpdateMemoryInfo
.计时器由 .NET 运行时保持活动状态,直到它停止.计时器会在您的弹出窗口中保留一个引用,因为您使用实例方法作为事件处理程序.您的弹出窗口通过 Parent
属性保持对网格的引用.网格通过其自己的 Parent
属性保持对页面的引用.所以你只是让你的页面不朽,只要你的计时器在滴答作响.要证明问题存在,只需将 UpdateMemoryInfo
方法设为静态(并删除其中的所有 UI 更新代码).由于事件处理程序现在是静态的,因此计时器不会保存对弹出实例的引用.运行分析器,您会看到页面实例现在已按预期被垃圾收集器回收.
In the CreatePopups
method, you create the popup and add it to the grid of the starting page.
In the popup, you start a timer to call UpdateMemoryInfo
at regular interval.
The timer is kept alive by the .NET runtime until it's stopped. The timer keeps a reference on your popup because you're using an instance method as event handler. Your popup is keeping a reference to the grid through the Parent
property. The grid is keeping a reference to the page through its own Parent
property. So you just made your page immortal, for as long as your timer is ticking.
To prove that the issue is there, just make the UpdateMemoryInfo
method static (and remove all the UI updating code there's inside). Since the event handler is now static, the timer won't hold a reference to instance of popup. Run the profiler, and you'll see that the instance of the page is now reclaimed by the garbage collector as you expect.
当然,它假定您的页面已从后台堆栈中删除.通过按返回键或调用 NavigationService.GoBack()
方法,或使用 NavigationService.RemoveBackEntry()
手动删除它们(如果您只使用向前导航)
Of course, it supposes that your pages have been removed from the back stack. Either by pressing the back key or calling the NavigationService.GoBack()
method, or by manually removing them using NavigationService.RemoveBackEntry()
(in case you only use forward navigation)
这篇关于删除 Pages windows phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!