tracemalloc —- 跟踪内存分配
3.4 新版功能.
The tracemalloc module is a debug tool to trace memory blocks allocated byPython. It provides the following information:
Traceback where an object was allocated
Statistics on allocated memory blocks per filename and per line number:total size, number and average size of allocated memory blocks
Compute the differences between two snapshots to detect memory leaks
To trace most memory blocks allocated by Python, the module should be startedas early as possible by setting the PYTHONTRACEMALLOC environmentvariable to 1, or by using -X tracemalloc command lineoption. The tracemalloc.start() function can be called at runtime tostart tracing Python memory allocations.
By default, a trace of an allocated memory block only stores the most recentframe (1 frame). To store 25 frames at startup: set thePYTHONTRACEMALLOC environment variable to 25, or use the-X tracemalloc=25 command line option.
例子
显示前10项
显示内存分配最多的10个文件:
importtracemalloc
tracemalloc.start()
# ... run your application ...
snapshot=tracemalloc.take_snapshot()
top_stats=snapshot.statistics('lineno')
print("[ Top 10 ]")
forstatintop_stats[:10]:
print(stat)
Python测试套件的输出示例:
[Top10]
:716:size=4855KiB,count=39328,average=126B
:284:size=521KiB,count=3199,average=167B
/usr/lib/python3.4/collections/__init__.py:368:size=244KiB,count=2315,average=108B
/usr/lib/python3.4/unittest/case.py:381:size=185KiB,count=779,average=243B
/usr/lib/python3.4/unittest/case.py:402:size=154KiB,count=378,average=416B
/usr/lib/python3.4/abc.py:133:size=88.7KiB,count=347,average=262B
:1446:size=70.4KiB,count=911,average=79B
:1454:size=52.0KiB,count=25,average=2131B
:5:size=49.7KiB,count=148,average=344B
/usr/lib/python3.4/sysconfig.py:411:size=48.0KiB,count=1,average=48.0KiB
We can see that Python loaded 4855 KiB data (bytecode and constants) frommodules and that the collections module allocated 244 KiB to buildnamedtuple types.
计算差异
获取两个快照并显示差异:
importtracemalloc
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 ]")
forstatintop_stats[:10]:
print(stat)
Example of output before/after running some tests of the Python test suite:
[Top10differences]
:716:size=8173KiB(+4428KiB),count=71332(+39369),average=117B
/usr/lib/python3.4/linecache.py:127:size=940KiB(+940KiB),count=8106(+8106),average=119B
/usr/lib/python3.4/unittest/case.py:571:size=298KiB(+298KiB),count=589(+589),average=519B
:284:size=1005KiB(+166KiB),count=7423(+1526),average=139B
/usr/lib/python3.4/mimetypes.py:217:size=112KiB(+112KiB),count=1334(+1334),average=86B
/usr/lib/python3.4/http/server.py:848:size=96.0KiB(+96.0KiB),count=1(+1),average=96.0KiB
/usr/lib/python3.4/inspect.py:1465:size=83.5KiB(+83.5KiB),count=109(+109),average=784B
/usr/lib/python3.4/unittest/mock.py:491:size=77.7KiB(+77.7KiB),count=143(+143),average=557B
/usr/lib/python3.4/urllib/parse.py:476:size=71.8KiB(+71.8KiB),count=969(+969),average=76B
/usr/lib/python3.4/contextlib.py:38:size=67.2KiB(+67.2KiB),count=126(+126),