In object-oriented programming, a destructor (sometimes abbreviated dtor) is a method which is invoked mechanically just before the memory of the object is released. It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources (memory allocations, open files or sockets, database connections, resource locks, etc.) which were acquired by the object during its life and/or deregister from other entities which may keep references to it. Use of destructors is needed for the process of Resource Acquisition Is Initialization (RAII). With most kinds of automatic garbage collection algorithms, the releasing of memory may happen a long time after the object becomes unreachable, making destructors (called finalizers in this case) unsuitable for most purposes. In such languages, the freeing of resources is done either through a lexical construct (such as try..finally, Python's "with" or Java's "try-with-resources"), which is the equivalent to RAII, or explicitly by calling a function (equivalent to explicit deletion); in particular, many object-oriented languages use the Dispose pattern. Destructor syntax C++: destructors have the same name as the class with which they are associated, but with a tilde (~) prefix. D: destructors are declared with name ~this() (whereas constructors are declared with this()). Object Pascal: destructors have the keyword destructor and can have user-defined names, but are mostly named Destroy. Objective-C: the destructor method has the name dealloc. Perl: the destructor method has the name DESTROY; in the Moose object system extension, it is named DEMOLISH. PHP: In PHP 5+, the destructor method has the name __destruct. There were no destructors in prior versions of PHP. Python: there are __del__ methods called destructors by the Python 2 language guide, but they are actually finalizers as acknowledged in Python 3. Rust: the destructor method for rust has the name drop Swift: the destructor method has the name deinit. In C++ The destructor has the same name as the class, but with a tilde (~) before it. For example, a class called foo will have the destructor . Additionally, destructors have neither parameters nor return types. As stated above, a destructor for an object is called whenever the object's lifetime ends. If the object was created as an automatic variable, its lifetime ends and the destructor is