← Back to writing

June 21, 2021 · 2 min read

Engineering

Finding and deleting memory hungry temporary variables in jupyter notebooks

Finding and deleting memory hungry temporary variables in jupyter…

This simple command displays the name of the variables in the current kernel in Jupyter notebooks, whichi are clogging our memory resources the most.

import sys
def sizeof_fmt(num, suffix='B'):
    ''' by Fred Cirera,  https://stackoverflow.com/a/1094933/1870254, modified'''
    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)

for name, size in sorted(((name, sys.getsizeof(value)) for name, value in locals().items()), key= lambda x: -x[1])[:10]:
    print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))

Continue exploring

All articles ↗