Created
November 16, 2022 20:41
-
-
Save earonesty/71d4f25020026e21c7f039d786a996aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from tracemalloc import Snapshot | |
def main(): | |
# todo: argparser | |
fil = sys.argv[1] | |
num = 10 | |
if len(sys.argv) > 2: | |
num = int(sys.argv[2]) | |
snap = Snapshot.load(fil) | |
topmem(snap, num) | |
if snap.traceback_limit != 1: | |
tracemem(snap, range(num)) | |
def topmem(snap, num): | |
top_stats = snap.statistics('lineno') | |
print(f"[ Top {num} ]") | |
for stat in top_stats[:num]: | |
print(stat) | |
def tracemem(snap, nums): | |
top_stats = snap.statistics('traceback') | |
limit = snap.traceback_limit | |
print(f"[ Frame limit {limit} ]") | |
# todo: add filter cli args/config | |
for num in nums: | |
# pick the biggest memory block | |
stat = top_stats[num] | |
print(f"[ Top {num} ]") | |
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024)) | |
for line in stat.traceback.format(): | |
print(line) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment