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.
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).
Int -> (Int, Int)
mapping? In theory, calculating the size of such an object should be simple, right? –
Viborg 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.
object.__sizeof__()
? It seems that sys.getsizeof(object)
returns a slightly larger value. EDIT: The latter also includes the garbage collector overhead. –
Larder sys.getsizeof(object.copy)
often gives the correct value, where sys.getsizeof(object)
gives you some far lower value (I guess the pointer size). –
Ihab import sys
! –
Familiar (100000000,)
, sys.getsizeof(x.copy)
shows 80. But the data type is float64. –
Godwit 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).
Int -> (Int, Int)
mapping? In theory, calculating the size of such an object should be simple, right? –
Viborg I haven't any personal experience with either of the following, but a simple search for a "Python [memory] profiler" yield:
PySizer, "a memory profiler for Python," found at http://pysizer.8325.org/. However the page seems to indicate that the project hasn't been updated for a while, and refers to...
Heapy, "support[ing] debugging and optimization regarding memory related issues in Python programs," found at http://guppy-pe.sourceforge.net/#Heapy.
Hope that helps.
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>
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.
© 2022 - 2024 — McMap. All rights reserved.
__sizeof__(self)
for them. For example NumPy does that, anda.__sizeof__()
is somewhat bigger (includes the object overhead), thana.nbytes
- which is the number of bytes in the allocated array. – Godric