【问题标题】:Memory Leak when using Silverlight's TimePickers in MVVM application在 MVVM 应用程序中使用 Silverlight 的 TimePickers 时出现内存泄漏
【发布时间】:2011-01-05 09:05:46
【问题描述】:

在我们的 Silverlight MVVM 应用程序中,我们一直在努力使用 Silverlight Toolkit TimePicker。使用它会停止使用 TimePicker 进行垃圾收集的视图。当我打开和关闭同一个视图 5 次时,内存中有 5 个视图副本。

以前的内存泄漏问题我们已经在 KeyTriggers 中遇到过,其中包含 MVVM_Light:EventToCommand。这个问题是通过使用 KeyTrigger 的名称在代码中分离触发器来解决的。

使用 WinDbg 工具我们可以看到,在关闭 View 后,TimePicker 对 View(当然还有 ViewModel)有引用。 我现在的问题是,是否有可能以某种方式分离特定的 TimePicker 的 KeyTrigger 来解决这个问题?或者如果有人已经使用过 TimePicker,还有其他方法可以防止内存泄漏?

感谢您的帮助!!

PS:这是WinDbg的一个视图的!gcroot命令的输出:

    DOMAIN(07E099E8):HANDLE(Pinned):39b12f8:Root:  0bd74260(System.Object[])->
  0ad8a2d0(System.Collections.Generic.Dictionary`2[[System.IntPtr, mscorlib],[System.Object, mscorlib]])->
  0c0dc6e8(System.Collections.Generic.Dictionary`2+Entry[[System.IntPtr, mscorlib],[System.Object, mscorlib]][])->
  0b6fa01c(System.Windows.Controls.Border)->
  0b6fa080(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
      0b6fa0cc(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b6f985c(System.Windows.Controls.Grid)->
  0b6f98c0(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  0b6f9f90(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b6f9564(System.Windows.Controls.ContentControl)->
  0b6f97c8(System.Collections.Generic.Dictionary`2[[System.Windows.DependencyProperty, System.Windows],[System.Windows.EffectiveValueEntry, System.Windows]])->
  0b6f9814(System.Collections.Generic.Dictionary`2+Entry[[System.Windows.DependencyProperty, System.Windows],[System.Windows.EffectiveValueEntry, System.Windows]][])->
  0b6f97b4(MS.Internal.ModifiedValue)->
  0b6f9754(System.Windows.TemplateBindingExpression)->
  0b6f61a0(System.Windows.Controls.TimeUpDown)->
  0b6f874c(System.EventHandler`1[[System.Windows.Controls.UpDownParseErrorEventArgs, System.Windows.Controls.Input.Toolkit]])->
  0b49f9b4(System.Windows.Controls.TimePicker)->
  0b49fef4(System.Windows.DataContextChangedEventHandler)->
  0b49fea0(System.Windows.Data.BindingExpression)->
  0b44ff48(MyProject.ViewModel.MyViewModel)->
  0b450514(GalaSoft.MvvmLight.Command.RelayCommand`1[[System.String, mscorlib]])->
  0b707b1c(System.EventHandler)->
  0b4a7f84(GalaSoft.MvvmLight.Command.EventToCommand)->
  0b4a7c2c(System.Windows.DOCollection)->
  0b4a7bcc(System.Windows.Interactivity.TriggerActionCollection)->
  0b4a7b5c(Microsoft.Expression.Interactivity.Input.KeyTrigger)->
  0b4a4704(System.Windows.DOCollection)->
  0b4a5b8c(System.Collections.Generic.Dictionary`2[[System.Windows.DependencyObject, System.Windows],[System.Object, mscorlib]])->
  0b4a84b0(System.Collections.Generic.Dictionary`2+Entry[[System.Windows.DependencyObject, System.Windows],[System.Object, mscorlib]][])->
  0b4a7118(Microsoft.Expression.Interactivity.Input.KeyTrigger)->
  0b4a732c(System.Collections.Generic.Dictionary`2[[System.Windows.DependencyProperty, System.Windows],[System.Windows.EffectiveValueEntry, System.Windows]])->
  0b4a7378(System.Collections.Generic.Dictionary`2+Entry[[System.Windows.DependencyProperty, System.Windows],[System.Windows.EffectiveValueEntry, System.Windows]][])->
  0b70f5f4(MS.Internal.ModifiedValue)->
  0b4a2eb0(System.Windows.Controls.DataGrid)->
  0b7059e4(System.Windows.Controls.ScrollContentPresenter)->
  0b705a54(MS.Internal.Controls.ScrollData)->
  0b4a2bb8(System.Windows.Controls.ScrollViewer)->
  0b49b70c(System.Windows.Controls.Grid)->
  0b4a8b50(System.Windows.Controls.Grid)->
  0b48ebb0(System.Windows.Controls.Grid)->
  0b48ec14(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  0b492f3c(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b49b4b0(System.Windows.Controls.Grid)->
  0b49b514(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  0b49b560(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b493a28(System.Windows.Controls.Grid)->
  0b4947a8(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  0b4aa2f4(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b498900(System.Windows.Controls.Grid)->
  0b49912c(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  0b499178(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  0b498ad4(System.Windows.Controls.TextBox)->
  0b498b58(MS.Internal.CoreTypeEventHelper)->
  0b498d20(System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[MS.Internal.CoreTypeEventHelper+EventAndDelegate, System.Windows]])->
  0b498d6c(System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[MS.Internal.CoreTypeEventHelper+EventAndDelegate, System.Windows]][])->
  0b498d10(MS.Internal.CoreTypeEventHelper+EventAndDelegate)->
  0b498cd8(System.Windows.Input.KeyEventHandler)->
  0ae3cf10(MyProject.Views.MyView)

【问题讨论】:

    标签: silverlight windbg mvvm-light silverlight-toolkit


    【解决方案1】:

    Visual Studio 2010 SP1 支持 Silverlight 分析。您可以使用此实用程序并充分利用它。

    【讨论】:

      【解决方案2】:

      尝试使用 Antz 内存分析器 (RedGate) 并分析您的应用。它将为您提供确定它为何保留在内存中所需的所有信息。可能是事件处理程序没有被清理?

      我已经使用他们的分析器解决了很多内存泄漏问题。你必须自己清理的东西真是太棒了。

      【讨论】:

        【解决方案3】:

        使用KeyTriggerPopup会出现内存泄漏,MS会not fix it near future

        使用Window时不会出现该问题。

        【讨论】:

          猜你喜欢
          • 2012-04-30
          • 2012-05-25
          • 2021-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-09
          • 2013-11-02
          • 1970-01-01
          相关资源
          最近更新 更多