As we know that during run time garbage collection request automatically when there is not enough space in memory to forming object. In my knowledge garbage collection call automatically, but when we do some programming in real world then it become indespensible to call garbage collection.let a example if we are making a programm in first step i want to call garbage collection for optimizing memory for further creation of object that means during run time. So how we will call garbage collector ?
5 Answers
Generally you should not call GC.Collect yourself. The GC is self-tuning and will most likely do a much better job than you can as it can take account of the entire managed heap and its usage.
However, you may want to check the answers to this question as well.

- 1
- 1

- 114,645
- 34
- 221
- 317
GC.Collect()
Be aware though!
Generally .NET does a good job of managing the memory for you, and this should be used with care! IMO

- 6,899
- 1
- 43
- 51
You really should not ever have to do this. As long as you properly dispose of your objects you really have no need to invoke the GC. Also remember on any IDisposable object that you do not need to maintain a lifetime for, the using statement is your friend.

- 13,558
- 5
- 50
- 76
but when we do some programming in real world then it become indespensible to call garbage collection.
This is not true.
The .NET garbage collector does a very good job determining when it should run, what to free etc., it is adopted to a wide range of real-world scenarios and performs very well in those.
For emphasis, let me phrase this quite clear, as most of the people posting before me already have: In a typical application, there is not need to ever force the garbage collector to run manually. Forcing the GC to run will usually hinder performance, not improve it.

- 31,821
- 4
- 39
- 33
You can force the garbage collector to collect by calling GC.Collect()
, but I highly recommend against it.
Have a read of this question and answer: GC.Collect()

- 1
- 1

- 19,423
- 9
- 68
- 97