Windows 窗体应用程序中的内存泄漏 [英] Memory leaks in a Windows Forms application

查看:30
本文介绍了Windows 窗体应用程序中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个大型 .NET Windows 窗体应用程序.尽管我们正在处理表单,但我们仍面临内存泄漏/使用问题.

We are developing a big .NET Windows Forms application. We are facing a memory leak/usage problem in that despite we are disposing the forms.

场景是这样的:

  1. 我们的应用程序使用 60 KB 内存,并在网格中显示记录列表.
  2. 当用户点击记录时,它会打开一个表单,myform.showDialog,显示详细信息.内存跃升,从60 KB 到 105 MB.
  3. 现在我们关闭表单 myform 以返回到网格,并处理该表单并将其设置为空.内存保持105MB.
  4. 现在,如果我们再次执行第 2 步,它将从 105MB 跳到 150MB,依此类推.
  1. Our application is using 60 KB of memory with a list of records displaying in a grid.
  2. When the user clicks on a record it opens a form, myform.showDialog, show the details. The memory jumps from 60 KB to 105 MB.
  3. Now we close the form myform to get back to grid, and dispose that form and set it to null. Memory remains at 105 MB.
  4. Now if we again perform step 2, it would jump from 105 MB to 150 MB and so on.

当我们关闭myForm时如何释放内存?

How can we free up the memory when we close myForm?

我们已经尝试过GC.Collect()等,但是没有任何结果.

We have already tried GC.Collect(), etc., but without any result.

推荐答案

查找泄漏的第一个地方是事件处理而不是丢失 Dispose() 调用.假设您的容器(父表单)加载一个子表单并为该子表单的事件添加一个处理程序(ChildForm.CloseMe).

The first place to look for leaks is in event-handling rather than missing Dispose() calls. Say your container (the parent form) loads a child form and adds a handler for an event of that child form (ChildForm.CloseMe).

如果要从内存中清除子窗体,则必须在此事件处理程序成为垃圾收集的候选对象之前将其删除.

If the child form is intended to be cleared from memory then this event handler must be removed before it is a candidate for garbage collection.

这篇关于Windows 窗体应用程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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