News

Memory profiling

Written on 19.12.2017 15:18 by Rahul Gopinath

If you find that the memory usage of your Python program is increasing suspiciously, Python 3.4 and later has a new memory profiling API called tracemalloc. The tracemalloc module allows you to take a snapshot of your memory at various points, and compare them to determine which objects have grown.

An example from the Python documentation below:

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)

 

Privacy Policy | Legal Notice
If you encounter technical problems, please contact the administrators.