Memory Allocation?

Tim Hoffman timh at zute.net
Mon Feb 7 21:08:44 EST 2005


Hi Chris

Have a look at 
http://www.python.org/doc/2.3.4/whatsnew/section-pymalloc.html

for a description of what is going on.

Basically malloc is used to grab a big chunk of memory, then
python objects use bits of it.

Rgds

Tim

Chris S. wrote:
> Donn Cave wrote:
> 
>> 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?
> 
> 
> GC == Garbage Collector (http://docs.python.org/lib/module-gc.html)
> 
>> 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
> 
> 
> Are you referring to Python's general method of memory management? I was 
> under the impression that the ISO C specification for malloc() dictates 
> allocation of a fixed amount of memory. free(), not malloc(), handles 
> deallocation. Am I wrong? Does Python use a custom non-standard 
> implementation of malloc()?



More information about the Python-list mailing list