def show_memory(unit='KB', threshold=1):
'''查看变量占用内存情况
:param unit: 显示的单位,可为`B`,`KB`,`MB`,`GB`
:param threshold: 仅显示内存数值大于等于threshold的变量
'''
from sys import getsizeof
scale = {'B': 1, 'KB': 1024, 'MB': 1048576, 'GB': 1073741824}[unit]
for i in list(globals().keys()):
memory = eval("getsizeof({})".format(i)) // scale
if memory >= threshold:
print(i, memory)
if __name__ == '__main__':
a = [i for i in range(10000)]
show_memory()
# a 85
10000条数字的列表占用内存85KB