Finding memory leaks in Python with tracemalloc


Python tip:

You can use tracemalloc to display files allocating the most memory.

An example👇

import tracemalloc

import app

tracemalloc.start()

app.run()

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics("lineno")

for stat in top_stats[:10]:
    print(stat)