Garbage Collection in Python
Last Updated :
08 May, 2025
Garbage Collection in Python is an automatic process that handles memory allocation and deallocation, ensuring efficient use of memory. Unlike languages such as C or C++ where the programmer must manually allocate and deallocate memory, Python automatically manages memory through two primary strategies:
- Reference counting
- Garbage collection
Reference counting
Python uses reference counting to manage memory. Each object keeps track of how many references point to it. When the reference count drops to zero i.e., no references remain, Python automatically deallocates the object. Example:
Python
import sys
x = [1, 2, 3]
print(sys.getrefcount(x))
y = x
print(sys.getrefcount(x))
y = None
print(sys.getrefcount(x))
Explanation:
- x is referenced twice initially (once by x, once by getrefcount()).
- Assigning y = x increases the count.
- Setting y = None removes one reference.
Problem with Reference Counting
Reference counting fails in the presence of cyclic references i.e., objects that reference each other in a cycle. Even if nothing else points to them, their reference count never reaches zero. Example:
Python
import sys
x = [1, 2, 3]
y = [4, 5, 6]
x.append(y)
y.append(x)
print(sys.getrefcount(x))
print(sys.getrefcount(y))
Explanation:
- x contains y and y contains x.
- Even after deleting x and y, Python won’t be able to free the memory just using reference counting, because each still references the other.
Garbage collection for Cyclic References
Garbage collection is a memory management technique used in programming languages to automatically reclaim memory that is no longer accessible or in use by the application. To handle such circular references, Python uses a Garbage Collector (GC) from the built-in gc module. This collector is able to detect and clean up objects involved in reference cycles.
Generational Garbage Collection
Python’s Generational Garbage Collector is designed to deal with cyclic references. It organizes objects into three generations based on their lifespan:
- Generation 0: Newly created objects.
- Generation 1: Objects that survived one collection cycle.
- Generation 2: Long-lived objects.
When reference cycles occur, the garbage collector automatically detects and cleans them up, freeing the memory.
Automatic Garbage Collection of Cycles
Garbage collection runs automatically when the number of allocations exceeds the number of deallocations by a certain threshold. This threshold can be inspected using the gc module.
Python
import gc
print(gc.get_threshold())
Explanation: It returns the threshold tuple for generations 0, 1 and 2. When allocations exceed the threshold, collection is triggered.
Manual garbage collection
Sometimes it’s beneficial to manually invoke the garbage collector, especially in the case of reference cycles. Example:
Python
import gc
# Create a cycle
def fun(i):
x = {}
x[i + 1] = x
return x
# Trigger garbage collection
c = gc.collect()
print(c)
for i in range(10):
fun(i)
c = gc.collect()
print(c)
Explanation:
- def fun(i) creates a cyclic reference by making a dictionary reference itself.
- gc.collect() triggers garbage collection and stores the count of collected objects (initially 0).
- for i in range(10) calls fun(i) 10 times, creating 10 cyclic references.
- gc.collect() triggers garbage collection again and prints the count of collected cycles .
Types of Manual garbage collection
- Time-based garbage collection: The garbage collector is triggered at fixed time intervals.
- Event-based garbage collection: The garbage collector is called in response to specific events, such as when a user exits the application or when the application becomes idle.
Forced garbage collections
Python's garbage collector (GC) runs automatically to clean up unused objects. To force it manually, use gc.collect() from the gc module. Example:
Python
import gc
a = [1, 2, 3]
b = {"a": 1, "b": 2}
c = "Hello, world!"
del a,b,c
gc.collect()
Explanation:
- del a, b, c deletes references to a, b and c, making them eligible for garbage collection.
- gc.collect() forces garbage collection to free memory by cleaning up unreferenced objects.
Disabling garbage collection
In Python, the garbage collector runs automatically to clean up unreferenced objects. To prevent it from running, you can disable it using gc.disable() from the gc module. Example:
Python
import gc
gc.disable()
gc.enable()
Explanation:
- gc.disable() disables automatic garbage collection.
- gc.enable() re-enables automatic garbage collection.
Interacting with python garbage collector
A built-in mechanism called the Python garbage collector automatically eliminates objects that are no longer referenced in order to free up memory and stop memory leaks. The Python gc module offers a number of ways to interact with the garbage collector, which is often executed automatically.
1. Enabling and disabling the garbage collector: You can enable or disable the garbage collector using the gc. enable() and gc. disable() functions, respectively. Example:
Python
import gc
# Disable
gc.disable()
# Enable
gc.enable()
2. Forcing garbage collection: You can manually trigger a garbage collection using the gc. collect() function. This can be useful in cases where you want to force immediate garbage collection instead of waiting for automatic garbage collection to occur. Example:
Python
3. Inspecting garbage collector settings: You can inspect the current settings of the garbage collector using the gc.get_threshold() function, which returns a tuple representing the current thresholds for generations 0, 1, and 2. Example:
Python
import gc
t = gc.get_threshold()
print(t)
4. Setting garbage collector thresholds: You can set the thresholds for garbage collection using the gc.set_threshold() function. This allows you to manually adjust the thresholds for different generations, which can affect the frequency of garbage collection. Example:
Python
import gc
gc.set_threshold(500, 5, 5)
t = gc.get_threshold()
print(t)
Advantages and Disadvantages
Let's explore some of the benefits and drawbacks of Python's garbage collection.
Advantages | Disadvantages |
---|
Automatic Memory Management | May introduce performance overhead |
No Manual Memory Handling | Requires understanding of memory concepts |
Efficient Cleanup via Generations | Limited control over timing of GC |
Customizable GC Settings | Possibility of bugs or memory leaks |
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read