clear memory? how?

Terry Reedy tjreedy at udel.edu
Tue May 9 14:30:18 EDT 2006


"Diez B. Roggisch" <deets at nospam.web.de> wrote in message 
news:4cb49uF15811rU1 at uni-berlin.de...
> You don't - python does it for you. It is called garbage collection. All 
> you
> have to to is get into granny-mode(tm): forget about things.

In general, especially for beginners, this is good advice.
Forget about memory management until one actually runs into a problems.

 >That means: once an object is not referenced by your code anymore,
> it will be cleaned up.

The language spec only says that it *may* be cleaned up.  Even with 
CPython, it will not be immediately deleted if caught in a circular 
reference loop with other objects no longer referenced by the code.  One 
may then want to explicitly invoke the circular ref garbage collector.  And 
read the gc module docs for details.

> For example:
>>>> l = range(100000)
>>>> del l
> will make python garbage collect the list referred to by l.

No, it only *allows* the interpreter to gc the list and (most of - 
depending on the implementation) the ints in the list.  When, if ever, is 
implementation dependent.
Even with CPython, ranges much larger than that are better done as xranges 
unless the explicit all-at-once list is really needed.

> Generally speaking: don't worry.

Terry Jan Reedy






More information about the Python-list mailing list