Python works by using reference counting and a garbage collector of its own that reclaims back memory. It also manages its own heap memory, separately from the system heap. The heap is organized in fixed-size blocks, which are organized into pools, which are organized into arenas.
tracemalloc
is a python package that allows us to programatically collect information about the memory.
tracemalloc.take_snapshot()
will generate a snapshot element.
Snapshot
s can be compared with their compare_to()
method, which includes a key_type
, to group the results by filename
, lineno
or traceback
.
Example
Fast API endpoints
This will yield a result like the following when calling the GET /memory/snapshots
endpoint:
0.00 new KiB, 602.31 total KiB, 0 new blocks, 6161 total blocks
File "<frozen importlib._bootstrap_external>", line 0
0.00 new KiB, 264.03 total KiB, 0 new blocks, 18 total blocks
File "/usr/local/lib/python3.8/gzip.py", line 0
0.00 new KiB, 254.15 total KiB, 0 new blocks, 25 total blocks
File "/usr/local/lib/python3.8/site-packages/uvloop/__init__.py", line 0
0.00 new KiB, 179.01 total KiB, 0 new blocks, 1777 total blocks
File "/usr/local/lib/python3.8/abc.py", line 0
0.00 new KiB, 147.27 total KiB, 0 new blocks, 1510 total blocks
File "<frozen importlib._bootstrap>", line 0
0.00 new KiB, 134.92 total KiB, 0 new blocks, 1163 total blocks
File "/usr/local/lib/python3.8/site-packages/fastapi/utils.py", line 0
0.00 new KiB, 74.03 total KiB, 0 new blocks, 959 total blocks
File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 0
0.00 new KiB, 59.04 total KiB, 0 new blocks, 605 total blocks
File "/usr/local/lib/python3.8/site-packages/pytz/__init__.py", line 0
0.00 new KiB, 50.82 total KiB, 0 new blocks, 303 total blocks
File "/usr/local/lib/python3.8/site-packages/websockets/exceptions.py", line 0
0.00 new KiB, 48.31 total KiB, 0 new blocks, 709 total blocks
File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 0