Python Memory Management and Optimization Techniques
Python automatically manages memory through garbage collection, and this article explains how the garbage collector works and presents ten practical techniques—including manual gc calls, generators, sys.getsizeof, slice copying, del statements, sys.intern, NumPy, pandas, reference counting, and __slots__—to reduce memory usage and improve performance.
Python automatically manages memory using a garbage collector that detects and frees objects no longer in use, helping to release memory for other objects.
Below are ten practical techniques and code examples to further reduce memory consumption and improve performance.
1. Manual garbage collection with the gc module:
import gc
# execute some code
gc.collect() # manually trigger garbage collection2. Use a generator function instead of a list comprehension to lower memory usage:
def generator_function():
for i in range(1000000):
yield i
# Use the generator
data = generator_function()3. Retrieve an object's memory size with sys.getsizeof() :
import sys
data = [1, 2, 3, 4, 5]
size = sys.getsizeof(data)
print(size) # output the object's memory size4. Copy a list using slicing to avoid extra memory allocation:
data = [1, 2, 3, 4, 5]
copy = data[:]5. Delete objects that are no longer needed with the del statement:
data = [1, 2, 3, 4, 5]
# use data for some operations
del data # promptly delete the object6. Intern strings using sys.intern() to share identical string objects:
import sys
string = sys.intern("some_string")
# the string is now interned and shared in memory7. Replace Python lists with NumPy arrays for numerical computation:
import numpy as np
data = np.array([1, 2, 3, 4, 5])
result = np.sum(data)8. Use pandas DataFrames instead of plain lists or dictionaries for data processing:
import pandas as pd
data = pd.DataFrame({
'column1': [1, 2, 3],
'column2': ['a', 'b', 'c']
})
result = data.groupby('column2').sum()9. Check an object's reference count with sys.getrefcount() to detect circular references or leaks:
import sys
data = [1, 2, 3, 4, 5]
ref_count = sys.getrefcount(data)
print(ref_count) # output the reference count10. Limit instance attributes with __slots__ to reduce per‑object memory overhead:
class MyClass:
__slots__ = ['attribute1', 'attribute2']
def __init__(self):
self.attribute1 = 1
self.attribute2 = 2
obj = MyClass()These examples demonstrate practical Python memory‑management and optimization scenarios; by leveraging garbage collection, reducing memory footprints, and employing high‑performance libraries, developers can enhance program efficiency and lower memory consumption.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
