Run tracemalloc module as a script

It would be useful to have the ability to invoke tracemalloc as a script for tracing memory allocations in another script.

For example:
python -m tracemalloc [-o output_file] [-t key_type] [--cumulative] [-n number of frames] (-m module | myscript.py)

The following command could print top -n Statistic’s in this way:

<frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B
<frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 B
/usr/lib/python3.4/collections/__init__.py:368: size=244 KiB, count=2315, average=108 B
/usr/lib/python3.4/unittest/case.py:381: size=185 KiB, count=779, average=243 B
/usr/lib/python3.4/unittest/case.py:402: size=154 KiB, count=378, average=416 B
/usr/lib/python3.4/abc.py:133: size=88.7 KiB, count=347, average=262 B
<frozen importlib._bootstrap>:1446: size=70.4 KiB, count=911, average=79 B
<frozen importlib._bootstrap>:1454: size=52.0 KiB, count=25, average=2131 B
<string>:5: size=49.7 KiB, count=148, average=344 B
/usr/lib/python3.4/sysconfig.py:411: size=48.0 KiB, count=1, average=48.0 KiB

This approach will suffice for tracing memory allocation in most simple scenarios and make it more convenient, as it doesn’t require any changes in the source code.

Sounds like a good idea. Could you submit a PR for this ? Thanks.

Thank you for your opinion. I’ve created a topic to confirm this feature will be useful for others as well.

Sure, I can create a PR.

Do you think a PEP is required for this?