[egenix-users] Calc size of Python objects
M.-A. Lemburg
mal at lemburg.com
Tue Nov 12 12:29:58 CET 2002
Dirk Holtwick wrote:
> So this one would be more accurate? If I understand you right, it's not
> possible to calculate the exact amount of memory used by a python object?
>
> class myPyObject:
> pass
>
> def calcsize(i, s=0):
> s = sizeof(i)
> if type(i) == type({}):
> for k, v in i.items():
> s += calcsize(k)
> s += calcsize(v)
> s += 2 * sizeof(myPyObject())
Well, the table size will usually be around 3/2 of the number
of entries. For more details see the C implementation :-)
> elif type(i) == type([]):
> for v in i:
> s += calcsize(v)
> s += 1 * sizeof(myPyObject())
> elif type(i) == type((1,)):
> for v in i:
> s += calcsize(v)
> return s
>
> I don't know exctly how to determine the size of a PyObject.
import struct
sizeof_PyObject = len(struct.pack('P', 0))
On 32-bit machines that's usually 4 bytes.
> The proc.py is exactly what I was looking for and it's working fine.
> Thanks a lot Marc-Andre! Helps me tracking down some strange memory bugs
> in my application.
--
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting: http://www.egenix.com/
Python Software: http://www.egenix.com/files/python/
More information about the egenix-users
mailing list