WPF 内存使用情况 [英] WPF Memory Usage

查看:65
本文介绍了WPF 内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 由顶部的文本框和下方的列表框组成的 WPF 应用程序
  • 用户在TextBox中输入字符串查找员工,搜索结果显示在ListBox中
  • ListBox 使用 DataTemplates 来显示元素(显示员工姓名、部门、​​电话和缩略图.)
  • 在应用程序启动时,我查询数据库并检索所有员工和相关信息以显示在 ListBox 中.这会一直保存在内存中.
  • 应用程序启动后,所有可搜索的数据都在内存中,搜索几乎是即时的.所有搜索都在内存中的数据上执行.
  • 使用 DataTemplates 将搜索结果显示在 ListBox 中.缩略图、姓名、电话、部门等显示在每个 ListBox 项中.
  • 启动时内存使用量约为 200MB.
  • 随着列表框中的数据发生变化,无论是通过新搜索还是简单地向下滚动列表框,内存消耗都会增加.
  • 当用户缓慢向下滚动列表框时,内存增加得更快.当您上下滚动时,内存会迅速达到 1GB.

没有手动创建控件的代码 - 一切都是通过数据绑定完成的.

There are is no code creating controls manually - everything is done via data binding.

为什么我会看到这种行为?我能做些什么来修复它?请帮忙!

Why am I seeing this behavior? What can I do to fix it? Please help!

更新:我发现问题不是内存泄漏.这里的问题是列表框正在创建对象以显示员工的图像,并且在列表框项离开窗口后没有为垃圾收集器释放.CleanUpVirtualizedItem 事件如我所料发生,但内存仍未释放.有什么想法吗?

UPDATE: I figured out that the problem is not a memory leak. The issue here is that the listbox is creating objects to display the images of the employee and is not releasing for the garbage collector after the listboxitem gets out of the window. The CleanUpVirtualizedItem event occurs as I expected but the memory is still not released. Any ideas?

推荐答案

冒着被 glib 的风险,你有内存泄漏.为什么不试试像 ANTS* 这样的工具来追踪它.他们有免费试用版,我没用过,但口碑不错.

At the risk of being glib, you have a memory leak. Why not try a tool like ANTS* to track it down. They have a free trial, I've never used it but it has a good reputation.

*其他分析工具可用.

如果您不想使用其他工具,可以尝试在每次创建类时增加静态成员并在每次释放实例时减少它.这将帮助您追踪未正确销毁的实例.

If you don't want to get to grips with another tool, you can try something like incrementing a static member every time a class is created and decrementing it every time an instance is disposed. This will help you track down instances that are not be destroyed properly.

这篇关于WPF 内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆