Check for memory leaks in Python

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

How to use

View list of memory usage

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.

See the difference

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

Check for memory leaks in Python
Check for external commands in python
Search for strings in Python
Techniques for sorting in Python
About "for _ in range ():" in python
[Python] Generate QR code in memory
Check the operation of Python for .NET in each environment
Check and move directories in Python
Run unittests in Python (for beginners)
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Check the behavior of destructor in Python
Memory leak in Python Jupyter Lab (Notebook)?
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Check and receive Serial port in Python (Port check)
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Check if the URL exists in Python
Template for writing batch scripts in python
When looking at memory usage in Python 3
How to check opencv version in python
Process multiple lists with for in Python
MongoDB for the first time in Python
Find and check inverse matrix in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
SublimeText2 and SublimeLinter --Syntax check for Python3--
2016-10-30 else for Python3> for:
Check memory usage
Quadtree in Python --2
Python in optimization
python [for myself]
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python nan check
Meta-analysis in Python
python grammar check
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python