I want to know is there any difference between destructor and garbage collector, destructor is used to dispose of all unused objects at the end of the lifetime of the applic
The garbage collector primarily works by copying all the objects it can find to a new part of RAM, and then nuking the old area; it neither knows nor cares whether there were five or 500,000 objects left behind. Note that in addition to finding all objects referred to by live strong references, the garbage collector can find a few other objects as well, including objects which override Finalize
, objects used as monitor locks, objects targeted by WeakReference objects, etc. Before nuking the old area from orbit, the garbage collector must deal with any of 'special' objects it knows about that might still be sitting there.
Among other things, the garbage collector has a list of all objects that have registered a finalizer; it will checks all objects on that list to see if they've yet been copied to the new memory area. If any are found that haven't been, they'll be removed from the list of objects with a registered finalizer and added to a list of objects whose Finalize
method should be run as soon as practical. Once this has been done for all objects with a registered finalizer, any objects on the list of objects needing immediate finalization, as well as any object to which those objects holds a reference, will be copied to the new area.
The garbage collector is a part of the .NET environment that keeps track of objects and makes sure that objects are removed from memory when they are no longer needed.
A destructor is a part of a class design. It's the opposite of a constructor. When you declare it the GC will call it when it destroys an object.
Here is the MSDN documentation.
The destructor is a special member function which is invoked when an object is destroyed. It is the last method run by a class.
The garbage collector is part of the framework, automatically manages memory, and non-deterministically collects unreferenced objects to avoid memory leaks.
The garbage collector and finalizer/destructor are intrinsically linked - however, most objects do not need (and do not have) a destructor. They are actually very rare in managed code, and are usually used to ensure unmanaged resources are released. If an object has a destructor/finalizer, the garbage collector invokes it around the same time as collection (maybe in the next pass). Garbage collection is non-deterministic - it happens when it happens - often relating to memory pressure.
Far more common, however, is IDisposable. This allows a more predictable pattern for releasing resources now (rather than when GC next happens). Often, classes that have a finalizer will also be IDisposable, with the Dispose() implementation disabling the destructor (it isn't needed if we've already cleaned up). Note that Dispose() is unrelated to garbage collection, but has language support via the "using" statement.
IDisposable is much more common than finalizers. You are responsible for ensuring anything IDisposable gets disposed. Additional note: disposing something does not cause the object to get collected; that is done only by the GC on whatever schedule the GC chooses. Disposal, rather, release associated resources. As an example, you wouldn't want a file being locked open until GC happens; the Dispose() here unlocks the file (by releasing the OS file handle).