Tuesday, September 28, 2004

How to prevent memory leak in C#.NET?


If you are letting .NET to manage memory, it will do a good job of memory management. If you start messing with the GC then you get into big trouble.

.NET's GC does not collect items right away, it lingers around in case the block is needed somewhere else. It collects when it thinks it should.

Don't mess with memory management unless you have a really, really good reason to. The Dispose method is used to release unmanaged resources that aren't handled by the garbage collector, like some streams and graphics operations. If you are using these, then you'll want to call the Dispose method when you're done with the object. The object itself will remain in memory until it's collected, but the unmanaged resources associated with it will be freed.

Note: For most streams, the Dispose method will probably be replaced by the Close method, which does the same thing.



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

No comments: