Ema Il
Ema Il

Reputation: 417

sort tracemalloc statistics by size

I have this code:

def print_statistics(s: Snapshot, s2: Snapshot):
    stats = s2.compare_to(s, 'lineno')
    for stat in stats[:20]:
        print(stat)

def some_func()
    tracemalloc.start(50)
    s = tracemalloc.take_snapshot()
    .
    .
    Code
    .
    .
    s2 = tracemalloc.take_snapshot()
    print_statistics(s, s2)

and this is a sample of the print:

/usr/local/lib/python3.8/json/decoder.py:353: size=2672 B (+2672 B), count=33 (+33), average=81 B
/usr/local/lib/python3.8/site-packages/prometheus_client/values.py:15: size=1664 B (+1664 B), count=32 (+32), average=52 B
/usr/local/lib/python3.8/site-packages/prometheus_client/metrics.py:521: size=1304 B (+1304 B), count=17 (+17), average=77 B
/usr/local/lib/python3.8/site-packages/prometheus_client/values.py:16: size=1152 B (+1152 B), count=32 (+32), average=36 B
/usr/local/lib/python3.8/tracemalloc.py:185: size=13.2 KiB (+5328 B), count=281 (+111), average=48 B
/usr/local/lib/python3.8/json/decoder.py:353: size=5344 B (-2672 B), count=66 (-33), average=81 B
/usr/local/lib/python3.8/tracemalloc.py:102: size=5891 B (+2366 B), count=37 (+16), average=159 B

Am I missing something with how I read it? It seems that the order is pretty random.

What do I need to do to actually see the part of code that use the most memory?

Upvotes: 0

Views: 391

Answers (0)

Related Questions