Introduction
The garbage collector automatically tracks and reclaims objects allocated in managed memory. Periodically, it performs garbage collection to reclaim memory allocated to objects for which there are no valid references.
Usually, classes implement the Idisposable interface, which contains the IDisposable.Dispose method that performs resource management and cleanup tasks at specific time. Also, finalizers are implemented by overriding the Object.Finalize method. But, in C# or C++, destructor methods have turn into the actual override of Object.Finalize.
Here in the following program, I have a base class and one derived class. Constructors and destructors methods are neatly coded in both the classes. The program keeps watching on the memory usage at every step. It forces garbage collection using the Collect method.
GarbageCollector provides you a method called GetTotalMemory which returns the total memory in the system.
Garbage Collector searches for objects that are not reacheable by program and these objects are queued up in the freachable queue with their finalizers still be called.There is a dedicated high priority thread (the finalizer thread) managed by the CLR that keeps an eye on the freachable queue. When the queue is empty the finalizer thread sleeps, but when any objects are added to the queue it gets woken up and starts sequentially calling their finalizers.The method WaitForPendingFinalizers suspend the current thread until the thread processing the queue of finalizers has emptied that queue.
using System; using System.Runtime.InteropServices; class ParentClass { public static long mem; public ParentClass() { Console.WriteLine("Base Object Created!"); } public static void Main() { mem = GC.GetTotalMemory(false); Console.WriteLine( "Memory Before creating derived Object: " + mem.ToString()); derived d = new derived(); mem = GC.GetTotalMemory(false); Console.WriteLine( "Memory After object creation: " + mem.ToString()); GC.Collect(); GC.WaitForPendingFinalizers(); mem = GC.GetTotalMemory(false); Console.WriteLine( "Memory during GC.collect: " + mem.ToString()); } ~ParentClass() { mem = GC.GetTotalMemory(false); Console.WriteLine("Memory After disposing the Base: " + mem.ToString()); } } class derived : ParentClass { IntPtr handle = IntPtr.Zero; [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr hObject); public derived() { Console.WriteLine("Derived Object Created!"); }
~derived() { if (handle != IntPtr.Zero) CloseHandle(handle); mem = GC.GetTotalMemory(false); Console.WriteLine( "Memory After disposing derived: " + mem.ToString()); } }
The output of the above program is
C:\>gctest Memory Before creating derived Object: 18408 Base Object Created! Derived Object Created! Memory After object creation: 22504 Memory After disposing derived: 27616 Memory After disposing the Base: 27616 Memory during GC.Collect: 27616
C:\>
GetTotalMemory method
This method gives you the number of bytes currently allocated in managed memory. It takes one parameter, forceFullCollection, a Boolean value that, if true, indicates this method can wait a short while for garbage collection and finalization of objects before returning. The duration of the interval is an internally specified limit determined by the number of garbage collection cycles completed and the change in the amount of memory recovered between cycles. The garbage collector does not guarantee that all inaccessible memory is collected. The same program produces the following output when this parameter of GetTotalMemory is set to True.
C:\>gctest Memory Before creating derived Object: 18408 Base Object Created! Derived Object Created! Memory After disposing derived: 27616 Memory After disposing the Base: 27616 Memory After object creation: 22504 Memory during GC.Collect: 27616
C:\>
Summary
Garbage Collector facilitates the developer to cleanup the resources occupied by objects created during the program execution. It has full control over the garbage collection process and this sample is a little bit what you have to control over it.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|