You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a command-line interface to tracemalloc #159560
Today, to check memory allocations of a script with tracemalloc you have to modify the script itself: call tracemalloc.start(), take a snapshot at the right place, then print the statistics. For simple cases I think it would be convenient to run it the same way as cProfile, without touching the code:
Tracing starts right before the program runs. When it exits, a snapshot is taken and the top allocations that are still alive get printed, plus the total and the peak traced size:
$ python -m tracemalloc -l 5 script.py
Top 5 lineno allocations still alive at exit:
script.py:2: size=981 KiB, count=101, average=9942 B
<frozen runpy>:266: size=434 B, count=4, average=108 B
<frozen runpy>:102: size=400 B, count=1, average=400 B
<frozen runpy>:81: size=400 B, count=1, average=400 B
script.py:1: size=168 B, count=1, average=168 B
2 other: size=90 B, count=2
Total size: 982 KiB
Peak size: 983 KiB
Options:
-o OUTFILE dumps the snapshot instead of printing it, so it can be loaded later with Snapshot.load() ( same idea as cProfile -o )
-t key type for Snapshot.statistics(), lineno by default
--cumulative cumulative mode of Snapshot.statistics()
-n max number of frames, same as tracemalloc.start(nframe)
-l number of statistics to display, 10 by default
-m run a module, like python -m
The traced program gets the same sys.argv, __name__ and sys.path[0] as when it runs directly. Memory freed before exit only shows up in the peak size.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Activity