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

问题描述

我已经读了很多关于这一点,因为我一直在问到修复具有内存泄漏问题的C#应用​​程序,但我还没有发现这些2个问题的答案:

I've been reading a lot about this since I've been asked to fix a C# application that has memory leaking problems, but I haven't found an answer for these 2 issues:

考虑下面的代码:

   private static ArrayList list = new ArrayList();

   public void Function()
   {
     list.add(object1);
     list.add(object2);

     //didn't call clear() prior to reusing list
     list = new ArrayList();
   }



由于列表创建一个新的之前没有被清除,这会产生一些垃圾不会自己配置静态列表之后被释放?

Since the list wasn't cleared before creating a new one, will this generate some sort of garbage that won't be released after the static list itself is disposed?

第二个问题是关于Form.Dispose()。我看到了很多关于设计视图中提供控件(即标签,图片框)要求处置。它好像调用Dispose()在窗体上导致所有这些类型的需要也设置控件(纠正我,如果我错了),这是奇怪,因为设计师增加了一个被覆盖的无效的Dispose(BOOL处置) 方法,它确实没有这样的事情。我猜想,这发生在无效的Dispose(BOOL处置)基Form类的方法。

The second issue is regarding Form.Dispose(). I see that a lot of controls available on designer view (i.e. labels, picture boxes) require disposing. It's seems as though calling Dispose() on a Form causes all of these types of controls to be disposed also (correct me if I'm wrong), which is odd since the designer adds an overriden void Dispose(bool disposing) method which does no such thing. I'm assuming that this happens at the void Dispose(bool disposing) method of the base Form class.

与上面的问题是这是我不太清楚什么,我需要做什么来确保所有形式的资源,正确处理。我不明白的形式如何知道它需要处置的对象。例如,如果在我的表单我有一个字段,它是一个自定义的IDisposable对象,将表单知道它需要处理?或者我应该补充必要的代码,以释放对象自己?

The problem with the above is that it is not very clear to me what I need to do to ensure that all of the Form's resources are disposed correctly. I do not understand how the Form knows which objects it needs to dispose. For example, if in my form I have a field which is a custom IDisposable object, will the Form know that it needs disposing? Or should I add the code necessary to release the object myself?

另外,如果我确实需要添加代码来处理特定的对象,那么我该如何处理事实上,设计师已经重写在无效的Dispose(BOOL处置)方式?我应该编辑设计器生成的代码还是有可以做到这一点更清洁的方式?

Also, if I do need to add the code to dispose certain objects, then how do I deal with the fact that the designer has already overriden the void Dispose(bool disposing) method? Should I edit the designer generated code or is there a cleaner way to do this?

我希望这不是混乱的,这是一个有点难以解释。谢谢

I hope that this wasn't to confusing, it's a bit hard to explain. Thanks

推荐答案

没有,这不是一个泄漏。当垃圾收集器进入搜索对象引用,它将找不到到原来的ArrayList一个参考了。你取而代之。因此,它会自动销毁原来的ArrayList对象,以及所有元素,如果他们没有被引用的任何地方无论是。

No, that's not a leak. When the garbage collector goes searching for object references, it won't find a reference to the original ArrayList anymore. You replaced it. So it will automatically destroy that original ArrayList object, as well as all of its elements if they are not referenced anywhere either.

Form类知道如何处理本身,以及属于该表上的子窗口的所有控件。这种情况发生在用户关闭形式,即Windows发送触发该代码WM_CLOSE消息。该Form.Controls集合帮助它找到引用到所有子控件,以便它能够处理它们。

The Form class knows how to dispose itself, as well as all the controls that are child windows on that form. This happens when the user closes the form, the WM_CLOSE message that Windows sends triggers this code. The Form.Controls collection helps it find the reference to all child controls so it can dispose them too.

不过,如果从形式自行删除控件不会出现这种情况。现在,它是由你来调用Dispose()在他们身上。特别是Controls.Clear()方法是危险的。什么是不寻常的是,这会导致永久性的泄漏,为您免除被停车窗口,更让控制。这使窗口的句柄活着,所以你可以将它们移动到其他位置上,例如另一个容器窗口。如果你没有真正移动它们,它们会永远留托管,停车窗口。没有其他类的框架颇有行为这种方式。

However, this will not happen if you remove controls from the form yourself. Now it is up to you to call Dispose() on them. Particularly the Controls.Clear() method is dangerous. What is unusual about it is that this causes a permanent leak, the controls you remove are kept alive by the 'parking window'. Which keeps the window handle alive so you can move them elsewhere, on another container window for example. If you don't actually move them, they'll stay hosted on that parking window forever. No other classes in the framework quite behave this way.

这泄漏容易Taskmgr.exe中诊断,进程选项卡。查看+选择列,并勾选用户对象。如果这个稳步上升,而你的程序然后运行你泄漏的控制。

This leak is easy to diagnose with Taskmgr.exe, Processes tab. View + Select Columns and tick USER Objects. If this steadily goes up while your program runs then you're leaking controls.

这篇关于内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 06:08