Garbage Collection in Python
Python uses automatic memory deallocation and allocation system. Similar to employing dynamic memory allocation in languages like C or C++, the user does not need to preallocate or deallocate memory.
Python supports two methods for allocating memory:
- Garbage collection
- Reference counting
Python 2.0, the Python interpreter’s memory management strategy was limited to reference counting. The process of reference counting involves keeping track of how frequently one object is referred to by another object inside the system. The reference count for an item decreases when references to it are dropped. The object is deallocated when the reference count reaches zero.
Reference Cycle
When an object’s reference count cannot be reached, a reference cycle is produced. Reference cycles involving dictionaries, functions, instances, classes, lists, and tuples are frequent. Making an object that makes self-references, as in the example below, is the simplest approach to start a reference cycle.
Example:
def first_class_cycle():
list_item = [ ]
list_item.append(list_item)
print(list_item)
first_class_cycle()
Output:
[[...]]
The object list_item will not immediately be returned when the method is finished because “first_class_cycle()” generates an object list_item that contains a self-referential reference. This will result in list_item‘s memory use being retained until the Python garbage collector is called.
Reference Counting
There are currently two references in the list that was constructed. But since it cannot be accessed from within Python and is therefore useless, it is regarded as garbage. This list is never released in Python as it is currently configured.
Example:
x = []
x.append(1)
x.append(2)
print(x)
del x
print(x)
Output:
[1, 2]
Traceback (most recent call last):
File "/Desktop/Python/Enablegeek.py", line 8, in <module>
print(x)
NameError: name 'x' is not defined
Python Self-Garbage Collector
Garbage collection needs to be a scheduled task since reference cycles require computational labor to find. Based on a threshold of object allocations and deallocations, Python schedules garbage collection. The garbage collector is launched when the sum of the allocations and deallocations exceeds a certain threshold. By importing the gc module and requesting the garbage collection thresholds, one can examine the threshold for brand-new objects (also known as generation 0 objects in Python):
Example:
import gc
print("Garbage collection thresholds:",gc.collect())
Output:
Garbage collection thresholds: 0
Manual Garbage Collection
When memory is being used up by reference cycles, it can be a good idea to manually invoke the garbage collector during program execution.
The following procedure can be used to manually start the garbage collection:
Example:
import gc
count = 0
def first_class_cycle():
list_item = [0]*5
list_item[count] = list_item
print(list_item)
garbage = gc.collect()
print("Garbage collector: collected %d objects." % garbage)
print("Creating cycles...")
for count in range(5):
first_class_cycle()
garbage = gc.collect()
print("Garbage collector: collected %d objects." % garbage)
Output:
Garbage collector: collected 0 objects.
Creating cycles...
[[...], 0, 0, 0, 0]
[0, [...], 0, 0, 0]
[0, 0, [...], 0, 0]
[0, 0, 0, [...], 0]
[0, 0, 0, 0, [...]]
Garbage collector: collected 5 objects.
Time-based and event-based garbage collection are the two methods used for manual garbage collection. Simple time-based garbage collection involves calling the garbage collector after a certain amount of time. The garbage collector is called when an event-based garbage collection occurs. For instance, when a user closes the application or when it enters a state of inactivity.