.Net C# Force garbage collection

A quick post on how to force garbage collection to take place.

using System;

namespace CsharpForceGarbageCollectionDemo
{
    class CsharpForceGarbageCollectionExample
    {
        static void Main(string[] args)
        {
            //Create an anonymous object
            var obj = new {Name = "Object", Contains = new[] {"A","B"}};

            GC.Collect();

            GC.WaitForPendingFinalizers();

            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *