Perform Action when a Python object is destroyed

I am working on a python program, which deals with quite some lot of class objects. 

It has been observed that in case the objects are not handled, especially destroyed properly, the overall program is abnormally terminated.

I am in search of some method/function which can be used to perform some specific actions such as releasing of memory, saving the required attributed, when a python class object is destroyed.

Please guide how this can be done.