From 55d4db51a0406063f0f73ad814b3c3fb5c827f13 Mon Sep 17 00:00:00 2001 From: JJJHolscher Date: Fri, 8 Dec 2023 11:32:40 +0100 Subject: [PATCH] memory.py --- src/memory.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/memory.py diff --git a/src/memory.py b/src/memory.py new file mode 100644 index 0000000..bd745d8 --- /dev/null +++ b/src/memory.py @@ -0,0 +1,29 @@ +#! /usr/bin/env python3 +# vim:fenc=utf-8 + +""" +Functions for showing which variables take up memory. +by Fred Cirera, https://stackoverflow.com/a/1094933/1870254, modified +""" + +import sys + +def sizeof_fmt(num, suffix='B'): + ''' Pretty print the size of a variable. ''' + for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: + if abs(num) < 1024.0: + return "%3.1f %s%s" % (num, unit, suffix) + num /= 1024.0 + return "%.1f %s%s" % (num, 'Yi', suffix) + +def print_largest_variables(top=10): + """ Print the names and sizes of the top largest variables. """ + variables = list(locals().items()) + variables_with_sizes = ((name, sys.getsizeof(value)) for name, value in variables) + + if not top: + for name, size in sorted(variables_with_sizes, key= lambda x: -x[1]): + print("{:>30}: {:>8}".format(name, sizeof_fmt(size))) + else: + for name, size in sorted(variables_with_sizes, key= lambda x: -x[1])[:top]: + print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))