The difference between a destructor and a garbage collector

I want to know if there is a difference between the destructor and the garbage collector, the destructor is used to dispose of all unused objects at the end of the life of the application, the same is the use of the garbage collector, the garbage collector can be manually called or executed at the end of the application, the same with destructor, both are optional and use to delete an object without links, can anyone point me to what exactly the difference

+8
garbage-collection destructor
source share
4 answers

A destructor is a special member function that is called when an object is destroyed. This is the last method executed by the class.

The garbage collector is part of the framework, automatically manages memory, and collects unpublished objects non-deterministically to avoid memory leaks.

+6
source share

The garbage collector and the finalizer / destructor are inextricably linked - however, most objects do not need (and do not have) a destructor. In fact, they are very rare in managed code and are commonly used to ensure the release of unmanaged resources. If the object has a destructor / finalizer, the garbage collector calls it at about the same time as the collection (possibly in the next pass). Garbage collection is non-deterministic - this happens when it happens - often due to memory pressure.

However, IDisposable is more common. This allows a more predictable pattern for allocating resources now (rather than when executing a GC). Often classes that have a finalizer will also be IDisposable, while the Dispose () implementation disables the destructor (it is not needed if we have already cleared it). Please note: Dispose () is not related to garbage collection, but has language support through the "using" statement.

IDisposable is much more than finalizers. You are responsible for ensuring that everything that is IDisposable is deleted. Additional note: deleting something does not lead to the collection of the object; this is done only by the GC according to any schedule selected by the GC. Elimination, rather, the release of related resources. For example, you do not want the file to be locked until the GC happens; Dispose () here unlocks the file (releasing the file descriptor of the OS).

+6
source share

The garbage collector is part of the .NET environment that monitors objects and ensures that objects are deleted from memory when they are no longer needed.

A destructor is part of the class design. This is the opposite of the constructor. When you declare it, the GC will call it when it destroys the object.

Here is the MSDN documentation.

+3
source share

The garbage collector basically works by copying all the objects that it can find into a new part of RAM, and then destroys the old area; he does not know or care about whether five or 500,000 objects remain. Note that in addition to searching for all objects referenced by live strong links, the garbage collector can also find several other objects, including objects that override Finalize , objects used as monitor locks, objects destined for WeakReference objects, etc. . Before destroying an old area from orbit, the garbage collector must deal with any “special” objects that he knows that he can still sit there.

Among other things, the garbage collector has a list of all the objects that the finalizer registered; it checks all the objects in this list to see if they were still copied to the new memory area. If any are found, they will be removed from the list of objects with a registered finalizer and added to the list of objects, the Finalize method should be started as soon as possible. As soon as this is done for all objects with a registered finalizer, any objects in the list of objects that need immediate improvement, as well as any object to which these objects contain a link, will be copied to a new area.

0
source share

All Articles