clear memory? how?

N

N/A

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.

Thank u!
 
D

Diez B. Roggisch

N/A said:
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:

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


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
 
A

Andrew Gwozdziewycz

Glad to see you're trying Python instead of Matlab. Python was never
meant to be a replacement for Matlab. It's a general purpose
programming language like Java, or C#. Though it has very little in
common with either of those languages. Since Python is a general
purpose language, if you want Matlab like functionality, you need to
rely on some other libraries that exist out there. Matplotlib, NumPy
to name a few will make your Matlab- to Python transition go much
more smoothly. You might also want to check out ipython, which is
just a different interface to the python toplevel.


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.

Thank u!

---
Andrew Gwozdziewycz
(e-mail address removed)
http://www.23excuses.com
http://ihadagreatview.org
http://and.rovir.us
 
B

Ben C

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.

I think Matlab's "clear all" is more like what you might call "del all"
in python.

You could perhaps define it like this:

def clearall():
all = [var for var in globals() if var[0] != "_"]
for var in all:
del globals()[var]

This deletes any global not starting with an _, since it's probably
inadvisable to delete this lot:

{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'/etc/pythonstart', '__name__': '__main__', '__doc__': None}

More correct I suppose might be something like this:

def clearall():
all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
for var in all:
del globals()[var]

since I think magic things always start and end with __.

Looking briefly at GNU octave which is similar to MatLab, clear all may
also del all the locals; so you can do something similar with the
builtin function locals().
 
B

Ben C

def clearall():
all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
for var in all:
del globals()[var]

since I think magic things always start and end with __.

Oops, got that wrong anyway:

should be:

all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")]
 
T

Terry Reedy

Diez B. Roggisch said:
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:
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
 
?

=?ISO-8859-1?Q?Gr=E9goire_Dooms?=

Ben said:
def clearall():
all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
for var in all:
del globals()[var]

since I think magic things always start and end with __.

Oops, got that wrong anyway:

should be:

all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")]

You can also add
and var != "clearall"

:)
 
B

Ben C

Ben said:
def clearall():
all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
for var in all:
del globals()[var]

since I think magic things always start and end with __.

Oops, got that wrong anyway:

should be:

all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")]

You can also add
and var != "clearall"

:)

Good point :)

I've heard of "write once run anywhere", but this is "run once".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top