PDA

View Full Version : How to invoke or force run garbage collector in .Net??


DotNet
16-01-2010, 06:29 PM
First thing first, garbage collector is a piece of code written for .net framework, so that any object which is not destroyed by the developers, can be taken care of. This is done to solve the biggest issue, programmers usually face, the Memory Leak problem.

So, how to get it working. Simple. You don't need to. .Net framework is smart enough to understand when a lot of objects are not being destroyed properly, and run this piece of code automatically. Basically its like a maid which cleans up your room without asking.

But what if you still feel that this smartness of .Net garbage collector is not enough, and you still need to tell it when to do the housekeeping for you. Its very simple. Just one line of code and its done. All that you have to do is, add this line in your code where you feel to force garbage collector to run.

System.GC.Collect();

That's it. Nothing else to do. Sometimes I really wonder programming can't be any simpler than this. What do you think?