clear memory? how?

Diez B. Roggisch deets at nospam.web.de
Tue May 9 11:04:55 CEST 2006


N/A wrote:

> Hi all,
> I am learning Python. Just wondering how to clear saved memory in
> Python? Like in Matlab I can simply use "clear all" to clear all saved
> memory.

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. That means:
once an object is not referenced by your code anymore, it will be cleaned
up.

For example:

>>> l = range(100000)
>>> del l

will make python garbage collect the list referred to by l. Actually, this
will work too:


>>> l = range(100000)
>>> l = None

Because l now refers another object (None in this case), the _list_ that was
refered beforehand gets freed as it's reference counter is reduced by one.

Generally speaking: don't worry.

HTH,

Diez




More information about the Python-list mailing list