memory recycling/garbage collecting problem

Terry Reedy tjreedy at udel.edu
Thu Feb 26 19:22:12 CET 2009


Steve Holden wrote:
> David Niergarth wrote:
>> Tim Peters showed a way to demonstrate the fix in
>>
>> http://mail.python.org/pipermail/python-dev/2006-March/061991.html
>>
>>> For simpler fun, run this silly little program, and look at memory
>>> consumption at the prompts:
>>>
>>> """
>>> x = []
>>> for i in xrange(1000000):
>>>    x.append([])
>>> raw_input("full ")
>>> del x[:]
>>> raw_input("empty ")
>>> """
>>>
>>> For example, in a release build on WinXP, VM size is about 48MB at the
>>> "full" prompt, and drops to 3MB at the "empty" prompt.  In the trunk
>>> (without this patch), VM size falls relatively little from what it is
>>> at the "full" prompt (the contiguous vector holding a million
>>> PyObject* pointers is freed, but the obmalloc arenas holding a
>>> million+1 list objects are never freed).
>>>
>>> For more info about the patch, see Evan's slides from _last_ year's PyCon:
>>>
>>>     http://evanjones.ca/memory-allocator.pdf
>> I'm not sure what deleting a slice accomplishes (del x[:]); the
>> behavior is the same whether I do del x or del x[:]. Any ideas?
>>
> del x removes the name x from the current namespace, garbage collecting
> the object to which it referred.

If there is another reference to the list, which there well might be in 
an actual application with memory problems, then 'del x' only 
disassociates the name but the object and its large contents are not gc'ed.

 > del x[:] leaves x referencing a cleared list.

which is guaranteed to be cleared, regardless of other refs.

Terry




More information about the Python-list mailing list