Categories: python

Garbage Collection in Python

  • In Python, garbage collection refers to the process of automatically freeing up memory that is no longer being used by the program.
  • This is done by the Python interpreter’s built-in garbage collector, which periodically scans the program’s memory and identifies objects that are no longer being used by any part of the program.
  • These objects are then deleted, and their memory is freed for other uses.
  • The exact details of how the garbage collector works are implementation-dependent, but it is generally based on reference counting, which keeps track of how many references to an object exist in the program at any given time.
  • When the reference count for an object reaches zero, it is considered unreferenced and eligible for garbage collection.
Automatic Garbage collection:

In Python, automatic garbage collection is performed by the built-in garbage collector, which runs periodically as the program executes. The garbage collector uses a technique called reference counting to determine which objects in memory are no longer being used by the program and can be safely deleted.

It’s important to note that the garbage collector only frees up memory, it doesn’t release any resources like file handlers, sockets, etc. and it’s the developer’s responsibility to release these resources.

Example:

Here is an example of how automatic garbage collection works in Python:

# Create an object and assign it to a variable
obj = SomeObject()

# The object's reference count is now 1

# Assign the same object to another variable
obj2 = obj

# The object's reference count is now 2

# Delete one of the variables
del obj

# The object's reference count is now 1

# Delete the second variable
del obj2

# The object's reference count is now 0, and it is eligible for garbage collection

In this example, we create an object of the class SomeObject and assign it to the variable obj. The object’s reference count is now 1. We then assign the same object to another variable, obj2, which increments the object’s reference count to 2.

We then delete the variable obj, which decrements the object’s reference count to 1. Finally, we delete the variable obj2, which decrements the object’s reference count to 0. At this point, the object is no longer referenced by any part of the program and is eligible for garbage collection.

It’s important to note that the garbage collector may not run immediately after an object’s reference count reaches zero. It runs periodically; it may take some time before the garbage collector identifies and frees the unreferenced object.

Forced Garbage Collection:

In Python, the built-in garbage collector runs periodically in the background to free up memory that is no longer being used by the program. However, it is not guaranteed to run at a specific time, and it may take some time before the garbage collector identifies and frees unreferenced objects.

You can force the garbage collector to run immediately by calling the gc.collect() method. This method runs the garbage collector and returns the number of unreachable objects that were collected.

Example:

For example, you can use the following code to force the garbage collector to run:

# Importing gc module
import gc

# Returns the number of
# objects it has collected
# and deallocated
collected = gc.collect()

# Prints Garbage collector
# as 0 object
print("Garbage collector: collected",
  "%d objects." % collected)
Output:
('Garbage collector: collected', '0 objects.')

It’s important to note that the Python garbage collector is a reference-counting collector, which means that it only reclaims memory for objects that have a reference count of zero. Additionally, the gc.collect() function might not be able to reclaim all the memory, due to some cycles of objects that are not referenced anymore but still can’t be deleted by the garbage collector.

Python also has a built-in trash list where it keeps track of all objects that are not referenced anymore. You can use gc.garbage to see the trash list.

Note: also read about Assert Statement in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago