Memory Allocation?

Donn Cave donn at u.washington.edu
Mon Feb 7 12:40:01 EST 2005


In article <8GCNd.9880$ya6.9761 at trndny01>,
 "Chris S." <chrisks at NOSPAM.udel.edu> wrote:

> Is it possible to determine how much memory is allocated by an arbitrary 
> Python object? There doesn't seem to be anything in the docs about this, 
> but considering that Python manages memory allocation, why would such a 
> module be more difficult to design than say, the GC?

Sorry, I didn't follow that - such a module as what?

Along with the kind of complicated internal implementation
details, you may need to consider the possibility that the
platform malloc() may reserve more than the allocated amount,
for its own bookkeeping but also for alignment.  It isn't
a reliable guide by any means, but something like this might
be at least entertaining -

   >>> 
   >>> class A:
   ...     def __init__(self, a):
   ...             self.a = a
   ... 
   >>> d = map(id, map(A, [0]*32))
   >>> d.sort()
   >>> k = 0
   >>> for i in d:
   ...     print i - k
   ...     k = i
   ... 

This depends on the fact that id(a) returns a's storage
address.

I get very different results from one platform to another,
and I'm not sure what they mean, but at a guess, I think
you will see a fairly small number, like 40 or 48, that
represents the immediate allocation for the object, and
then a lot of intervals three or four times larger that
represent all the memory allocated in the course of creating
it.  It isn't clear that this is all still allocated -
malloc() doesn't necessarily reuse a freed block right
away, and in fact the most interesting thing about this
experiment is how different this part looks on different
platforms.  Of course we're still a bit in the dark as
to how much memory is really allocated for overhead.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list