c# .net

Can we force garbage collector to run ?

Can we force garbage collector to run ?, someone asked me to explain?

System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if situations arises.

using System;

class Program


        private const int maxGarbage = 1000;

        static void Main()


            // Put some objects in memory.


            Console.WriteLine("Memory used before collection: {0:N0}",



            // Collect all generations of memory.


            Console.WriteLine("Memory used after full collection: {0:N0}",




        static void MakeSomeGarbage()


            Version vt;

            // Create objects and release them to fill up memory with unused objects.

            for (int i = 0; i < maxGarbage; i++)


                vt = new Version();





Memory used before collection:       193,252


Memoryused after full collection:   94,020

Post your comments / questions