Destrutores methods serve to release the memory allocated dynamically by the class, to eliminate references to it, when it does not exist.
In programming languages that have
Garbage Collector , it is not necessary to use destructor methods, because
Garbage Collector is in charge of doing this.
The need to create them, is in cases where the language does not have
Garbage Collector , and it becomes necessary to destroy the class after its use, so that it does not occupy memory.
In languages that have
Garbage Collector , only use is necessary when using unmanaged resources.
Most common types of unmanaged resources are objects that involve features of the operating system, such as files, windows, network connections, or database connections. Microsoft
In these cases
Garbage Collector does not know how to release and clean the unmanaged resource.
Not all languages use the
~ sign to designate a destructor.
PHP , for example,
__ is used:
void __destruct ( void )