I had to hand over a simple memory leak investigation work procedure and request it, so I summarized it here as well
I used guppy and heapy, but tracemalloc is standard in 3.4 series, and I can take the difference between snapshots, so I think it's okay to choose this one in the future.
Install and use the pytracemalloc module up to 3.3 series
import tracemalloc
tracemalloc.start()
# ... run your application ...
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
The number of allocated blocks and the size are given for each line allocated memory with malloc.
This is enough to understand the problem
import tracemalloc
tracemalloc.start()
# ... start your application ...
snapshot1 = tracemalloc.take_snapshot()
# ... call the function leaking memory ...
snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot1, 'lineno')
print("[ Top 10 differences ]")
for stat in top_stats[:10]:
print(stat)
Since the difference between the number of blocks and the size of each line where malloc ran is displayed, the memory that is not released even if the function is executed appears as the difference.
Recommended Posts