在.NET内存泄漏 [英] Memory leaks in .NET

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

问题描述

什么是所有中,我们可以得到内存泄漏在.NET的可能途径?

What are all the possible ways in which we can get memory leaks in .NET?

我知道有两种:

  1. 在不正确未注册事件处理程序/委托
  2. 在Windows窗体不处理动态子控件:

例如:

// Causes Leaks  
Label label = new Label();  
this.Controls.Add(label);  
this.Controls.Remove(label);  

// Correct Code  
Label label = new Label();  
this.Controls.Add(label);  
this.Controls.Remove(label);  
label.Dispose();

更新:我们的想法是要列出不是太明显(如上述)常见的陷阱。通常的观点是,内存泄漏是因为垃圾收集器不是一个大问题。不喜欢它曾经是C ++。

Update: The idea is to list common pitfalls which are not too obvious (such as the above). Usually the notion is that memory leaks are not a big problem because of the garbage collector. Not like it used to be in C++.

大讨论的家伙,但让我澄清......根据定义,如果没有参考留给了.NET对象时,它会被垃圾回收的一段时间。所以这不是一个方式诱导内存泄漏。

Great discussion guys, but let me clarify... by definition, if there is no reference left to an object in .NET, it will be Garbage Collected at some time. So that is not a way to induce memory leaks.

在托管环境中,我想如果你有一个意外引用任何对象,你是不知道的(因此这两个在我的问题的例子),认为这是一个内存泄漏。

In the managed environment, I would consider it a memory leak if you had an unintended reference to any object that you aren't aware of (hence the two examples in my question).

那么,什么是在这样的内存泄漏可能发生的各种可能的方式?

推荐答案

阻止终结器线程。没有其他对象将是垃圾收集,直到终结器线程是畅通的。因此,使用的内存会不断地成长量。

Block the finalizer thread. No other objects will be garbage collected until the finalizer thread is unblocked. Thus the amount of memory used will grow and grow.

延伸阅读: http://dotnetdebug.ne​​t/2005/06/ 22 /阻塞的终结线程/

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

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