DEV Community

Alex Chen
Alex Chen

Posted on

Quick Tip: Find What's Eating Your RAM with 4 Lines of Python

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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

python #coding #tips

Top comments (0)