在 MVVM 应用程序中使用 Silverlight 的 TimePickers 时出现内存泄漏

发布于 2024-10-11 02:21:01 字数 5620 浏览 3 评论 0原文

在我们的 Silverlight MVVM 应用程序中,我们在 Silverlight 工具包 TimePicker 上挣扎了一段时间。使用它会停止使用 TimePicker 的视图进行垃圾收集。当我打开和关闭同一个视图 5 次时,内存中有该视图的 5 个副本。

以前的内存泄漏问题我们已经在使用内部带有 MVVM_Light:EventToCommand 的 KeyTriggers 时遇到过。这个问题是通过使用 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)

In our Silverlight MVVM application we're struggling for some time with the Silverlight Toolkit TimePicker. Using it stops the View in which is used the TimePicker to be garbage collected. When I open and close 5 times the same view, there are 5 copies of the View in the memory.

Former problems with memory leaks we alredy had with KeyTriggers with an MVVM_Light:EventToCommand inside. This problem was solved by detaching the trigger in code behind by using the name of the KeyTrigger.

Using the WinDbg tool we can see that after closing the View there are references on the View (and of course also the ViewModel) from the TimePicker.
My question is now, whether it is possible in some way to detach the particular TimePicker's KeyTrigger to solve this problem? Or if someone had already experience in using the TimePicker, there is another way to prevent the memory leak?

Thank you for your help!!

PS: Here is the output of WinDbg's !gcroot command of one View:

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情妓 2024-10-18 02:21:01

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

Visual Studio 2010 SP1 support profiling for Silverlight. You can us this utiilty and get the best out of it.

窗影残 2024-10-18 02:21:01

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

我使用他们的分析器解决了很多内存泄漏问题。令人惊奇的是你必须自己清理这些东西。

Try the Antz memory profiler (RedGate) and profile your app. It will give you all the info you need to determine why it's remaining in memory. Possibly an event handler not getting cleaned up?

I've solved so many memory leaks using their profiler. It's amazing the stuff you have to clean up yourself.

半透明的墙 2024-10-18 02:21:01

特殊使用KeyTriggerPopup存在内存泄漏,MS将近期不会修复

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

There is memory leak with special using KeyTrigger and Popup, and MS will not fix it near future.

The problem does not occur when Window is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文