WPF RichTextBox 选项卡选择占用系统内存!
我在 WPF / MVVM 中有一个 TabControl,它绑定到 ObservableCollection 之上的 ItemsSource。 每个选项卡都有自己的集合和与图像、Richtextbox 和用户输入框等组件的绑定,并且一切似乎都运行良好。
然而,我注意到每次切换选项卡时,它都会使用大约 100k 的系统内存,而这些内存永远不会被回收! 如果我按住 ctrl-tab 循环浏览所有选项卡,我可以在一分钟内用完 200 兆内存。
现在 - 我创建了一个空白的 WPF 应用程序,仅包含一个选项卡控件,并且它还为每个选项卡开关使用内存(尽管少得多)。 这只是一些 .NET 错误还是一项功能? 也许它存储了面包屑路径,也许它用于调试(尽管我在发布模式下编译)。
我该如何恢复记忆? 或者更好的是,不会因选项卡切换而丢失内存?
I have a TabControl in WPF / MVVM that is bound to an ItemsSource on top of an ObservableCollection. Each tab has its own collections and bindings with components such as Images, Richtextboxes, and user input boxes, and everything seems to work well.
However, I have noticed that each time I switch a tab, it uses about 100k of system memory which never gets reclaimed! If I hold ctrl-tab down to cycle through all tabs, I can use up 200 megs of memory within a minute.
Now - I created a blank WPF app with just a tab control, and it also uses memory for each tab switch (albiet MUCH less). Is this just some .NET bug, or a feature? Maybe it stores a breadcrumb trail, maybe its used for debugging (although I compiled in release mode).
How do I reclaim my memory? Or better yet, not lose memory to tab switchings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这解决了我的问题:
http://blingcode.blogspot.com/2010/10 /memory-leak-with-wpfs-richtextbox.html
基本上为每个 RichTextBox 添加两个属性:) :)
IsUndoEnabled=“假”UndoLimit=“0”
This fixed the issue to me:
http://blingcode.blogspot.com/2010/10/memory-leak-with-wpfs-richtextbox.html
Basically add two attributes to each RichTextBox :) :)
IsUndoEnabled="False" UndoLimit="0"
您可能想检查是否有遗留的事件处理程序。
如果您在其他控件中注册了事件,垃圾收集器将不会收集不再需要的对象,因为可以说该事件仍然附加。
因此,如果您在代码后面
或 XAML 或 ParentControl 中的
某处注册了 Loaded ,您基本上有两种解决方案来解决此问题:
解决方案 1 - 当不再需要事件处理程序时将其删除:
您可能想要删除这个处理程序在父控件的卸载中如下所示:
当然,您也可以使用子控件的 Unloaded 事件..这取决于您..
解决方案 2 - 使用 WeakEvent 模式:
另一个事件的解决方案是 WeakEvent 模式,它绕过了这个问题。
无论哪种情况,祝你好运..很难找到像您遇到的那样的泄漏!
You might want to check if there are any event handlers left behind.
In the case where you registered an event in other control, the garbage collector will not collect the objects that are no longer needed, because the event is still attached so to speak.
So if you registered Loaded somewhere in code behind
or in XAML or the ParentControl
You basically have two solutions to tackle this problem:
Solution 1 - Remove event handlers when they are no longer needed:
You might want to remove this handler in the unloaded of the parent control like so:
You can also use the Unloaded event of the child control of course.. that is up to you..
Solution 2 - Using the WeakEvent pattern:
Another solution for events would be the WeakEvent pattern, which bypasses this problem.
In either case, good luck.. its quite hard to find leaks like the one you are experiencing!
很好的建议,但最终我认为可绑定文本块比 Richtextbox 更有用、更简单。 我从未发现是什么导致了泄漏 - 但它肯定是在 binablerichtextbox 代码中(每次切换选项卡时都会调用 OnInitialized,这超出了我的控制范围)。
泄漏消失了,并且由于使用了更简单的可绑定文本块,我的应用程序运行得更快。
Good advice, but in the end I decided a bindable textblock was more useful and simpler than a richtextbox. I never found out what was causing the leak - but it was definitely in the bindablerichtextbox code (OnInitialized was being called each time a tab switched, which was beyond my control).
The leaks are gone, and my app runs quicker because of the use of the simpler bindable textblock.