Actually python will reclaim the memory which is not in use anymore.This is called garbage collection which is automatic process in python. But still if you want to do it then you can delete it by del variable_name
. You can also do it by assigning the variable to None
a = 10
print a
del a
print a ## throws an error here because it's been deleted already.
The only way to truly reclaim memory from unreferenced Python objects is via the garbage collector. The del keyword simply unbinds a name from an object, but the object still needs to be garbage collected. You can force garbage collector to run using the gc module, but this is almost certainly a premature optimization but it has its own risks. Using del
has no real effect, since those names would have been deleted as they went out of scope anyway.