Use the psutil
util is a simple way to measure memory usage in Python scripts.
import os, psutil
def log_memory():
process = psutil.Process(os.getpid())
mem = process.memory_info().rss / (1024 * 1024)
print(f"Memory usage: {mem:.2f} MB")
Just invoke log_memory()
where you need. rss
is the “non swapped physical memory a process has used”.