找出Python中的对象使用了多少内存

发布于 2024-07-04 17:04:07 字数 75 浏览 10 评论 0原文

您将如何找出对象使用了多少内存? 我知道可以找出代码块使用了多少,但实例化对象(在其生命周期内的任何时间)使用了多少,这正是我想要的。

How would you go about finding out how much memory is being used by an object? I know it is possible to find out how much is used by a block of code, but not by an instantiated object (anytime during its life), which is what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喵星人汪星人 2024-07-11 17:04:08

必须谨慎使用,因为对对象 __sizeof__ 的覆盖可能会产生误导。

使用 bregman.suite,一些使用 sys.getsizeof 的测试会输出对象实例中数组对象(数据)的副本,该副本大于对象本身 (mfcc)。

>>> mfcc = MelFrequencyCepstrum(filepath, params)
>>> data = mfcc.X[:]
>>> sys.getsizeof(mfcc)
64
>>> sys.getsizeof(mfcc.X)
>>>80
>>> sys.getsizeof(data)
80
>>> mfcc
<bregman.features.MelFrequencyCepstrum object at 0x104ad3e90>

This must be used with care because an override on the objects __sizeof__ might be misleading.

Using the bregman.suite, some tests with sys.getsizeof output a copy of an array object (data) in an object instance as being bigger than the object itself (mfcc).

>>> mfcc = MelFrequencyCepstrum(filepath, params)
>>> data = mfcc.X[:]
>>> sys.getsizeof(mfcc)
64
>>> sys.getsizeof(mfcc.X)
>>>80
>>> sys.getsizeof(data)
80
>>> mfcc
<bregman.features.MelFrequencyCepstrum object at 0x104ad3e90>
不回头走下去 2024-07-11 17:04:08

没有简单的方法可以找出 python 对象的内存大小。 您可能会发现的问题之一是 Python 对象(例如列表和字典)可能引用其他 Python 对象(在这种情况下,您的大小是多少?大小是否包含每个对象的大小?)。 有一些与对象类型和垃圾收集相关的指针开销和内部结构。 最后,一些 Python 对象具有不明显的行为。 例如,大多数情况下,列表为比其拥有的对象更多的对象保留空间; 字典甚至更加复杂,因为它们可以以不同的方式操作(它们对少量键有不同的实现,有时它们会过度分配条目)。

有一个一大块代码(还有一个更新了一大块代码),试图最好地估计内存中 python 对象的大小。

您可能还想检查一些有关 PyObject 的旧描述 (代表几乎所有Python对象的内部C结构)。

There's no easy way to find out the memory size of a python object. One of the problems you may find is that Python objects - like lists and dicts - may have references to other python objects (in this case, what would your size be? The size containing the size of each object or not?). There are some pointers overhead and internal structures related to object types and garbage collection. Finally, some python objects have non-obvious behaviors. For instance, lists reserve space for more objects than they have, most of the time; dicts are even more complicated since they can operate in different ways (they have a different implementation for small number of keys and sometimes they over allocate entries).

There is a big chunk of code (and an updated big chunk of code) out there to try to best approximate the size of a python object in memory.

You may also want to check some old description about PyObject (the internal C struct that represents virtually all python objects).

谁的新欢旧爱 2024-07-11 17:04:08

对于大物体,你可以使用一个有点粗糙但有效的方法:
检查你的Python进程在系统中占用了多少内存,然后删除该对象并进行比较。

这种方法有很多缺点,但它可以让您非常快速地估计非常大的物体。

For big objects you may use a somewhat crude but effective method:
check how much memory your Python process occupies in the system, then delete the object and compare.

This method has many drawbacks but it will give you a very fast estimate for very big objects.

不寐倦长更 2024-07-11 17:04:07

试试这个:

sys.getsizeof(object)

getsizeof() 返回对象的大小字节。 如果对象由垃圾收集器管理,它会调用对象的 __sizeof__ 方法并添加额外的垃圾收集器开销。

递归配方

Try this:

sys.getsizeof(object)

getsizeof() Return the size of an object in bytes. It calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

A recursive recipe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文