Introduction
Understanding and optimizing Python memory usage for better performance.
Memory Profiling
# Basic memory usage
import sys
lst = [1, 2, 3]
print(sys.getsizeof(lst)) # Size in bytes
# Memory profiler
# pip install memory_profiler
from memory_profiler import profile
@profile
def my_function():
data = [i ** 2 for i in range(100000)]
return data
Garbage Collection
import gc
# Collect garbage
gc.collect()
# Get garbage collection stats
print(gc.get_stats())
# Find unreachable objects
print(gc.garbage)
# Disable automatic collection
gc.disable()
gc.enable()
Memory Efficient Patterns
# Use generators instead of lists
def get_lines(file):
for line in open(file):
yield line
# Use __slots__ in classes
class Point:
__slots__ = ["x", "y"]
# Use arrays for numerical data
from array import array
arr = array("i", [1, 2, 3, 4, 5])
Practice Problems
- Profile memory usage
- Find memory leaks
- Optimize data structures
- Use generators for large files
- Implement slots in classes