[Python-ideas] clear() method for lists
Stephen J. Turnbull
stephen at xemacs.org
Fri Feb 12 16:44:54 CET 2010
Gerald Britton writes:
> I was also wondering about garbage collection. If I have a
> humongous list, e.g. and "clear" it with:
> mylist = 
> does the old content not need to be garbage collected?
thislist = generate_me_a_humongous_list()
thatlist = thislist
thatlist = 
Definitely no garbage collection.
The starting point of using garbage collection is that in general you
don't know *locally* whether something is reachable or not. So you
need to do a global analysis.
> OTOH do dict.clear() and set.clear() immediately free their memory
> or does it just get queued for garbage collection?
This is covered in the manuals, but the gist is that every Python
object knows how many other objects are pointing to it (called a
refcount). When an object's refcount drops to zero, it gets collected
(immediately, IIRC). However ...
thislist = 
thatlist = [thislist]
and you have a reference cycle. These cycles are also collected, but
this requires more effort, and so it is done only occasionally.
More information about the Python-ideas