在.NET内存泄漏内存、NET

由网友(心痛是爱你的代价)分享简介:什么是所有中,我们可以得到内存泄漏在.NET的可能途径?What are all the possible ways in which we can get memory leaks in .NET?我知道有两种:在不正确未注册事件处理程序/委托。在Windows窗体不处理动态子控件:例如:// Causes...

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

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

我知道有两种:

在不正确未注册事件处理程序/委托。 在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 /阻塞的终结线程/

阅读全文

相关推荐

最新文章