Why Trust Techopedia

What Does Destructor Mean?

A destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following:

  • Recovering the heap space allocated during the lifetime of an object
  • Closing file or database connections
  • Releasing network resources
  • Releasing resource locks
  • Other housekeeping tasks

Techopedia Explains Destructor

Destructors are called explicitly in C++. However, in C# and Java this is not the case, as the allocation and release of memory allocated to objects are implicitly handled by the garbage collector. While destructors in C# and Java (called finalizers) are nondeterministic, C# destructors are guaranteed to be called by the .NET run time. However, Java finalizers have to be explicitly invoked since their invocation is not guaranteed.

Key properties of destructors can be summarized as follows:

  • Automatic invocation and no explicit call from user code
  • Overloading or inheritance not allowed
  • Access modifiers or parameters not to be specified
  • Order of call to destructor in a derived class is from the most derived to the least derived
  • Called not only during the object destruction, but also when the object instance is no longer eligible for access
  • Used in classes but not structs
  • Used only to release expensive unmanaged resources (like windows, network connection, etc.) that the object holds, rather than for releasing managed references

Related Terms

Margaret Rouse
Senior Editor
Margaret Rouse
Senior Editor

Margaret is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages.