如何在Python中获得对象在内存中占用的大小?


当前回答

Pympler包的asizeof模块可以做到这一点。

使用方法如下:

from pympler import asizeof
asizeof.asizeof(my_object)

不像系统。Getsizeof,它适用于你自己创建的对象。它甚至可以与numpy一起工作。

>>> asizeof.asizeof(tuple('bcd'))
200
>>> asizeof.asizeof({'foo': 'bar', 'baz': 'bar'})
400
>>> asizeof.asizeof({})
280
>>> asizeof.asizeof({'foo':'bar'})
360
>>> asizeof.asizeof('foo')
40
>>> asizeof.asizeof(Bar())
352
>>> asizeof.asizeof(Bar().__dict__)
280
>>> A = rand(10)
>>> B = rand(10000)
>>> asizeof.asizeof(A)
176
>>> asizeof.asizeof(B)
80096

正如前面提到的,

可以通过设置option code=True来包含类、函数、方法、模块等对象的(字节)代码大小。

如果你需要实时数据的其他视图,请选择Pympler

模块muppy用于在线监控Python应用程序 和模块类跟踪器提供的生命周期的离线分析 选择Python对象。

其他回答

下面是我根据之前对所有变量的列表大小的回答编写的一个快速脚本

for i in dir():
    print (i, sys.getsizeof(eval(i)) )

这可能比看起来要复杂得多,这取决于你想要如何计数。例如,如果您有一个int类型的列表,您是否需要包含对int类型引用的列表的大小?(即-列表,而不是包含在其中的内容),或者你想包括实际指向的数据,在这种情况下,你需要处理重复引用,以及如何防止重复计数当两个对象包含对同一对象的引用时。

您可能想要查看python内存分析器之一,例如pysizer,以查看它们是否满足您的需求。

If you don't need the exact size of the object but roughly to know how big it is, one quick (and dirty) way is to let the program run, sleep for an extended period of time, and check the memory usage (ex: Mac's activity monitor) by this particular python process. This would be effective when you are trying to find the size of one single large object in a python process. For example, I recently wanted to check the memory usage of a new data structure and compare it with that of Python's set data structure. First I wrote the elements (words from a large public domain book) to a set, then checked the size of the process, and then did the same thing with the other data structure. I found out the Python process with a set is taking twice as much memory as the new data structure. Again, you wouldn't be able to exactly say the memory used by the process is equal to the size of the object. As the size of the object gets large, this becomes close as the memory consumed by the rest of the process becomes negligible compared to the size of the object you are trying to monitor.

如果性能不是问题,最简单的解决方案是pickle和测量:

import pickle

data = ...
len(pickle.dumps(data))

您可以使用下面提到的getSizeof()来确定对象的大小

import sys
str1 = "one"
int_element=5
print("Memory size of '"+str1+"' = "+str(sys.getsizeof(str1))+ " bytes")
print("Memory size of '"+ str(int_element)+"' = "+str(sys.getsizeof(int_element))+ " bytes")