Python Forum

Full Version: Understanding tracemalloc results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm trying to find a memory leak in my program and I'm using tracemalloc.
From my understanding, the idea of tracealloc is that since you call "start()", each time you are calling "take_snapshot()" you will get the total amount of memory allocated, and then use "compare_to" between two full dumps.
But as it seems, this is not the case, as shown in this small example:

Output:
~ ยป ipython Python 3.7.7 (default, Mar 10 2020, 15:43:33) Type 'copyright', 'credits' or 'license' for more information IPython 7.15.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import tracemalloc In [6]: tracemalloc.start() In [7]: print(tracemalloc.take_snapshot().statistics('lineno')[0]) /Users/nudler/work/vision_client/venv/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py:563: size=88.7 KiB, count=1102, average=82 B In [8]: print(tracemalloc.take_snapshot().statistics('lineno')[0]) /Users/nudler/work/vision_client/venv/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py:563: size=52.4 KiB, count=627, average=86 B
As you can see, both "size" and "count" fields for the same line got decreased in the second call for "take_snapshot()".

What am I missing? Is "take_snapshot" returns the diff from the last call? (I got similar results from my actual code, size and count got decreased between two snapshots)

Thank you very much :)