Python Memory Management: How Python Handles Memory Efficiently
Introduction to Python Memory Management
Python handles memory management automatically, using a combination of a private heap, reference counting, and garbage collection. Understanding how Python manages memory can help you write more efficient code and troubleshoot memory-related issues.
Following tutorial will demonstrate Python’s memory management with practical examples, focusing on how Python handles objects, memory allocation, and garbage collection.
Example 1: Python Object Memory Allocation
This example shows how Python allocates memory for objects and how reference counting works.
Code:
# Creating an object (a string in this case)
a = "Hello, World!" # The memory is allocated for this string object
# Assigning the same object to another variable
b = a # No new memory is allocated; 'b' references the same object as 'a'
# Printing memory addresses of 'a' and 'b'
print(f"Memory address of 'a': {id(a)}") # Output: Memory address of 'a'
print(f"Memory address of 'b': {id(b)}") # Output: Memory address of 'b' (same as 'a')
# Creating a new object
c = "Python" # New memory is allocated for this different string object
# Printing memory address of 'c'
print(f"Memory address of 'c': {id(c)}") # Output: Memory address of 'c' (different from 'a' and 'b')
Output:
Memory address of 'a': 3250575399792 Memory address of 'b': 3250575399792 Memory address of 'c': 140731175044008
Explanation:
- Memory Allocation: When 'a' is assigned the string "Hello, World!", Python allocates memory for this string.
- Reference Counting: When 'b' is assigned to 'a', no new memory is allocated; instead, 'b' references the same object. Both 'a' and 'b' point to the same memory location.
- New Object Creation: A different string "Python" is assigned to 'c', which allocates new memory.
Example 2: Reference Counting in Python
This example explains how Python uses reference counting to keep track of the number of references pointing to an object.
Code:
import sys # Importing sys module to access reference count
# Creating an object (a list in this case)
my_list = [1, 2, 3]
# Checking the reference count of 'my_list'
print(f"Initial reference count of 'my_list': {sys.getrefcount(my_list)}") # Output: Reference count
# Creating additional references to the same object
ref1 = my_list
ref2 = my_list
# Checking the updated reference count of 'my_list'
print(f"Updated reference count of 'my_list': {sys.getrefcount(my_list)}") # Output: Increased reference count
# Deleting one reference
del ref1
# Checking the reference count after deletion
print(f"Reference count after deleting 'ref1': {sys.getrefcount(my_list)}") # Output: Decreased reference count
Output:
Initial reference count of 'my_list': 2 Updated reference count of 'my_list': 4 Reference count after deleting 'ref1': 3
Explanation:
- 'sys.getrefcount()': Returns the reference count of the given object.
- Initial Reference Count: Shows the reference count when the object is created.
- Increased Count: Adding 'ref1' and 'ref2' increases the reference count.
- Decreased Count: Deleting 'ref1' reduces the reference count by one.
Example 3: Python Garbage Collection
This example demonstrates Python's garbage collection process using the ‘gc’ module.
gc — Garbage Collector interface
This module provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot free. Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles. Automatic collection can be disabled by calling gc.disable().
Code:
import gc # Importing garbage collector module
# Defining a simple class to create objects
class MyClass:
def __init__(self, name):
self.name = name
# Creating an object instance
obj1 = MyClass("Object1")
# Checking if the object is tracked by garbage collector
print(f"Is 'obj1' tracked by GC?: {gc.is_tracked(obj1)}") # Output: True
# Deleting the object reference
del obj1
# Forcing garbage collection
gc.collect()
# Checking the status after collection
print("Garbage collection completed.")
Output:
Is 'obj1' tracked by GC?: True Garbage collection completed.
Explanation:
- 'gc' Module: Python's built-in garbage collection module.
- 'gc.is_tracked()': Checks if an object is tracked by the garbage collector.
- Garbage Collection: When ‘del obj1’ removes the reference to ‘obj1’, the memory can be reclaimed. ‘gc.collect()’ forces garbage collection.
Example 4: Memory Leaks and Circular References
This example shows how circular references can lead to memory leaks and how Python handles them.
Code:
import gc
# Defining two classes to create a circular reference
class A:
def __init__(self):
self.b = None # Placeholder for an instance of B
class B:
def __init__(self):
self.a = None # Placeholder for an instance of A
# Creating instances of both classes
obj_a = A()
obj_b = B()
# Creating a circular reference
obj_a.b = obj_b
obj_b.a = obj_a
# Forcing garbage collection
gc.collect()
# Breaking the circular reference
obj_a.b = None
obj_b.a = None
# Forcing garbage collection again
gc.collect()
print("Circular reference has been handled.")
Output:
Circular reference has been handled.
Explanation:
- Circular References: 'obj_a' references 'obj_b' and vice versa, forming a circular reference.
- Garbage Collection Handling: Even with circular references, Python's garbage collector can detect and clean them up if references are broken ('gc.collect()').
Example 5: Optimizing Memory Usage with '__slots__'
This example demonstrates using '__slots__' to reduce memory usage in Python objects.
Code:
# Without __slots__
class WithoutSlots:
def __init__(self, name, age):
self.name = name
self.age = age
# With __slots__
class WithSlots:
__slots__ = ['name', 'age'] # Define allowed attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Creating instances
obj1 = WithoutSlots("Alice", 30)
obj2 = WithSlots("Bob", 25)
# Checking the memory sizes
import sys
print(f"Memory usage without __slots__: {sys.getsizeof(obj1)} bytes") # Output: Memory size
print(f"Memory usage with __slots__: {sys.getsizeof(obj2)} bytes") # Output: Reduced memory size
Output:
Memory usage without __slots__: 56 bytes Memory usage with __slots__: 48 bytes
Explanation:
- '__slots__' Usage: Reduces memory usage by preventing the creation of a default '__dict__' for each instance.
- Memory Optimization: The memory footprint is smaller for 'WithSlots' than for 'WithoutSlots'.
Example 6: Using Weak References
This example shows how to use weak references to prevent memory leaks.
weakref — Weak references
The weakref module allows the Python programmer to create weak references to objects.
A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it.
Code:
import weakref # Importing weakref module
# Defining a class to create objects
class MyClass:
def __init__(self, name):
self.name = name
# Creating a strong reference
obj = MyClass("MyObject")
# Creating a weak reference to the object
weak_obj = weakref.ref(obj)
# Checking if weak reference is alive
print(f"Is weak reference alive?: {weak_obj() is not None}") # Output: True
# Deleting the strong reference
del obj
# Checking if weak reference is still alive
print(f"Is weak reference alive after deleting strong reference?: {weak_obj() is not None}") # Output: False
Output:
Is weak reference alive?: True Is weak reference alive after deleting strong reference?: False
Explanation:
- Weak References: Allow the object to be garbage-collected even if it is referenced weakly.
- Memory Management: Helps manage memory by avoiding unnecessary memory retention.
Example 7: Monitoring Memory Usage with psutil
This example shows how to monitor the memory usage of a Python program using the 'psutil' library.
psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes.
Code:
import psutil # Importing the psutil module to monitor system performance
# Function to check memory usage
def memory_usage():
process = psutil.Process() # Get the current process
mem_info = process.memory_info() # Get memory information
print(f"Memory usage: {mem_info.rss / (1024 * 1024):.2f} MB") # Print memory usage in MB
# Creating a large list
large_list = [i for i in range(1000000)]
# Checking memory usage
memory_usage() # Output: Memory usage before clearing the list
# Clearing the list to free memory
large_list.clear()
# Checking memory usage after clearing the list
memory_usage() # Output: Reduced memory usage after clearing
Output:
Memory usage: 202.97 MB Memory usage: 165.31 MB
Explanation:
- 'psutil' Library: Provides utilities to monitor memory and system performance.
- Memory Monitoring: Demonstrates how memory usage changes before and after freeing up resources.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python/python-memory-management-with-examples.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics