Memory leaks in .NET [closed]

That doesn’t really cause leaks, it just makes more work for the GC:

// slows GC
Label label = new Label();  
this.Controls.Add(label);  
this.Controls.Remove(label);  

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

// best
using( Label label = new Label() )
{ 
    this.Controls.Add(label);  
    this.Controls.Remove(label);  
}

Leaving disposable components lying around like this is never much of a problem in a managed environment like .Net – that’s a big part of what managed means.

You’ll slow you app down, certainly. But you won’t leave a mess for anything else.

Leave a Comment