微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,但没有任何成功.

调查

我已阅读以下内容解决此问题:
 StackOverFlow: Finalizer and Dispose

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐