python - 如何显示所有变量的大小
我想要同时打印出我当前作用域内所有变量的内存大小。
类似于这个:
for obj in locals().values():
print sys.getsizeof(obj)
不过我希望在每个值前面加上变量名,这样我就能看到哪些变量需要删除或者分成几个批次处理。
有什么想法吗?
3 个回答
0
我发现对于某些容器,我得不到正确的答案(只是开销部分吗?)。
结合@jan_Glx上面的回答和下面帖子中的一段代码
如何知道像数组和字典这样的Python对象的字节大小? - 简单的方法
from __future__ import print_function
from sys import getsizeof, stderr, getsizeof
from itertools import chain
from collections import deque
try:
from reprlib import repr
except ImportError:
pass
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)
def total_size(o, handlers={}, verbose=False):
""" Returns the approximate memory footprint an object and all of its contents.
Automatically finds the contents of the following builtin containers and
their subclasses: tuple, list, deque, dict, set and frozenset.
To search other containers, add handlers to iterate over their contents:
handlers = {SomeContainerClass: iter,
OtherContainerClass: OtherContainerClass.get_elements}
"""
dict_handler = lambda d: chain.from_iterable(d.items())
all_handlers = {tuple: iter,
list: iter,
deque: iter,
dict: dict_handler,
set: iter,
frozenset: iter,
}
all_handlers.update(handlers) # user handlers take precedence
seen = set() # track which object id's have already been seen
default_size = getsizeof(0) # estimate sizeof object without __sizeof__
def sizeof(o):
if id(o) in seen: # do not double count the same object
return 0
seen.add(id(o))
s = getsizeof(o, default_size)
if verbose:
print(s, type(o), repr(o), file=stderr)
for typ, handler in all_handlers.items():
if isinstance(o, typ):
s += sum(map(sizeof, handler(o)))
break
return s
return sizeof(o)
##### Example call #####
for name, size in sorted(((name, total_size(value, verbose=False)) for name, value in list(
locals().items())), key= lambda x: -x[1])[:20]:
print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))
138
这段代码稍微多一些,但在Python 3中能正常运行,并且输出的结果是排序过的,容易让人看懂:
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 list(
locals().items())), key= lambda x: -x[1])[:10]:
print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))
示例输出:
umis: 3.6 GiB
barcodes_sorted: 3.6 GiB
barcodes_idx: 3.6 GiB
barcodes: 3.6 GiB
cbcs: 3.6 GiB
reads_per_umi: 1.3 GiB
umis_per_cbc: 59.1 MiB
reads_per_cbc: 59.1 MiB
_40: 12.1 KiB
_: 1.6 KiB
请注意,这段代码只会打印出最大的10个变量,其他的就不显示了。如果你想要打印出所有的变量,只需把倒数第二行中的[:10]
去掉就可以了。
74
你可以通过使用 .items()
来同时遍历字典中的键和值。
from __future__ import print_function # for Python2
import sys
local_vars = list(locals().items())
for var, obj in local_vars:
print(var, sys.getsizeof(obj))