WPF内存使用率 [英] WPF Memory Usage

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

问题描述

  • WPF应用程序,由顶部的文本框和下方的列表框组成
  • 用户在文本框中键入一个字符串以查找员工,搜索结果显示在列表框中
  • 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.

*其他配置文件工具也可用.

*Other profiling tools are available.

如果您不想使用其他工具,可以尝试执行以下操作,例如,每次创建类时增加一个静态成员,而每次处置一个实例时减少一个静态成员.这将帮助您查找未正确销毁的实例.

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天全站免登陆