Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Destroying Objects (Garbage Collection) in Python
Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection.
Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.
How Reference Counting Works
An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope. When an object's reference count reaches zero, Python collects it automatically.
Reference Count Example
Here's how reference counting works in practice ?
a = 40 # Create object <40> b = a # Increase ref. count of <40> c = [b] # Increase ref. count of <40> del a # Decrease ref. count of <40> b = 100 # Decrease ref. count of <40> c[0] = -1 # Decrease ref. count of <40>
The __del__() Method
You normally will not notice when the garbage collector destroys an orphaned instance and reclaims its space. But a class can implement the special method __del__(), called a destructor, that is invoked when the instance is about to be destroyed. This method might be used to clean up any non-memory resources used by an instance.
Example
This __del__() destructor prints the class name of an instance that is about to be destroyed ?
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print(class_name, "destroyed")
pt1 = Point()
pt2 = pt1
pt3 = pt1
print(id(pt1), id(pt2), id(pt3)) # prints the ids of the objects
del pt1
del pt2
del pt3
The output of the above code is ?
140712234567808 140712234567808 140712234567808 Point destroyed
Checking Reference Count
You can check the reference count of an object using sys.getrefcount() ?
import sys
class MyClass:
pass
obj = MyClass()
print("Reference count:", sys.getrefcount(obj))
obj2 = obj # Create another reference
print("Reference count after assignment:", sys.getrefcount(obj))
Reference count: 2 Reference count after assignment: 3
Important Notes
When using the __del__() method, keep these points in mind:
- Circular references: Objects with circular references may not be garbage collected immediately
-
Exception handling: Avoid raising exceptions in
__del__()methods -
Resource cleanup: Use context managers or
try/finallyfor reliable resource cleanup
Conclusion
Python's garbage collection automatically manages memory through reference counting. The __del__() method allows custom cleanup when objects are destroyed, but should be used carefully for non-memory resources.
