Quick Tip
Your script's memory usage creeps up over hours and top only tells you the total. tracemalloc (stdlib, no install) shows you exactly which line is allocating:
import tracemalloc
tracemalloc.start()
# ... run the suspicious part of your code ...
snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics("lineno")[:5]:
print(stat)
Output points at the exact file/line allocating the most, e.g.:
/home/app/etl.py:142: size=312 MiB, count=1,048,576, average=312 B
/home/app/etl.py:98: size=48 MiB, count=12, average=4.0 MiB
Two more tricks:
# Compare two snapshots to find the leak between checkpoints
diff = snapshot2.compare_to(snapshot1, "lineno")
print(diff[0]) # biggest grower first
# Current vs peak usage
current, peak = tracemalloc.get_traced_memory()
print(f"current={current/1e6:.1f}MB peak={peak/1e6:.1f}MB")
Found a 300MB leak in 10 minutes with this last week — turned out to be a lru_cache on a method that keyed on mutable dicts (so nothing ever hit cache, and the dict keys piled up forever).
Powered by MonkeyCode: https://ly.cyberserval.tech/iIETXiF
Top comments (0)