c# – 为Windows phone Silverlight Pages实现Dispose和Finalize
我有一个关于图形,弹出窗口和动画的大解决方案.我发现在页面导航期间我有大量内存泄漏.
尝试 因此,我尝试了第一个解决方案: protected override void OnNavigatedTo(NavigationEventArgs e) { if (App.RootFrame.CanGoBack) App.RootFrame.RemoveBackEntry(); GC.Collect(); base.OnNavigatedTo(e); } 这来自MSDN和Stackoverflow上的几个来源,应该删除为页面存储的内存.情况并非如此,我不确定代码的MVVM结构是否以某种方式保存信息.然后我尝试实现解构器并在事件被触发时将值强制为null,如: ~SecondScreen() { In_Game_Crest = null; currentViewModel = null; } 这是我为所有页面,弹出窗口和用户控件所做的.然后我再次使用调试来完成代码,并且解析器的页面中没有被解雇.这导致我尝试使用IDisposable和fiddeling与MVVMLight提供的viewmodelLocator,但没有任何成功. 调查 我已阅读以下内容解决此问题: Finalize/Dispose pattern in C# MSDN: Implementing a Dispose Method MSDN: Implementing Finalize and Dispose to Clean Up Unmanaged Resources 问题 但它让我感到困惑,而不是帮助了我.我应该如何为我的Windows Phone实现页面的dispose和finalize方法? 由于我正在使用MVVM结构,这些方法应该在ViewModel中实现还是在给定页面后面或两者中实现? Windows手机的例子将非常感激. 初步尝试使用Dispose 我已经阅读了更多有关该主题的内容,并发现最终定型可能不应该写出来?但我仍然不确定.但基于此和上面的第一个MSDN链接,我尝试了以下内容: private bool disposed = false; public void Dispose() { Dispose(true); // Take yourself off the Finalization queue // to prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if(!this.disposed) { // If disposing equals true,dispose all managed // and unmanaged resources. if(disposing) { // Dispose managed resources. currentView = null; popup = null; Image1 = null; Image2 = null; } // Release unmanaged resources. If disposing is false,// only the following code is executed. this.Content = null; // Note that this is not thread safe. // Another thread could start disposing the object // after the managed resources are disposed,// but before the disposed flag is set to true. // If thread safety is necessary,it must be // implemented by the client. } disposed = true; } // Use C# destructor syntax for finalization code. // This destructor will run only if the Dispose method // does not get called. // It gives your base class the opportunity to finalize. // Do not provide destructors in types derived from this class. ~FirstPage() { Dispose(false); } protected override void OnNavigatedFrom(NavigationEventArgs e) { this.Dispose(); base.OnNavigatedFrom(e); } 但是,但是当我到达第二个屏幕时,这只会让我的记忆增加23MB.这再次引出了我的问题,我应该如何以及应该尝试实施什么,以及为什么内存会增加? this = null,base.Dispose() 我已经看到了不同的实现,在dispose函数中使用this = null或使用base.Dispose().我认为后者只能在类是IDisposable时使用?这是要走的路吗?如果是这样我该怎么办呢? 使用Microsoft Profiler 所以我使用分析器来验证FirstPage是否被删除 从上图可以看出第一页存在.在评论中,我被告知要查找实例并参考元素.因此我选择了firstpage的实例并得到了: 这里确认FirstPage永远不会被销毁.但是我被困在这里,我应该如何解释这些数据呢? 解决方法
当用户离开页面时实际上不需要处置,再次创建对象的性能影响大于在应用程序处于活动状态期间在内存中具有页面的内存负载.
您应该在从内存中删除对象之间做出决定.再次重新创建同一组对象. 说完这个之后你应该小心导航模型. 如果您在每次用户导航到页面时创建对象而在用户导航时不实际处置,则可能会出现内存问题. 为此,我建议您完全理解应用程序中的PageBase和NavigationHelper或NavigationService类. 你已经提到在应用程序的生命周期内没有从内存中删除FirstPage,根据我的说法是理想的. 将调试点放在代码中可能创建重对象的位置; 导航几次到不同的页面,然后回来. 检查行为,然后您可以自己清楚地了解情况. 对于所有对象,请检查您是否手动调用Dispose. Dispose是一个与GarbageCollector完全不同的概念,Dispose只是一个开发人员应该遵守的契约,通过调用它来释放他们认为不再需要在内存中维护的资源,因为平台的垃圾收集是在不确定的时间进行的. 在您发布的示例中,我看到您将对象设置为null而未实际处理.设置为null只会更改变量指向的内存位置. //Call on OnClosing or OnExit or similar context protected override void Dispose(bool isDisposing) { if(isDisposing && !_isDisposed){ if(disposeableImage != null){ disposeableImage.Dispose(); disposeableImage = null; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |