473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

clear memory? how?

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!
May 10 '06 #1
7 86009
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

May 10 '06 #2
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.
On May 9, 2006, at 4:27 AM, 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.

Thank u!
--
http://mail.python.org/mailman/listinfo/python-list


---
Andrew Gwozdziewycz
ap****@gmail.com
http://www.23excuses.com
http://ihadagreatview.org
http://and.rovir.us
May 10 '06 #3
On 2006-05-09, Diez B. Roggisch <de***@nospam.web.de> wrote:
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.


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().
May 10 '06 #4

"Diez B. Roggisch" <de***@nospam.web.de> wrote in message
news:4c*************@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

May 10 '06 #5
On 2006-05-09, Ben C <sp******@spam.eggs> wrote:
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:]) != ("__", "__")]
May 10 '06 #6
Ben C wrote:
On 2006-05-09, Ben C <sp******@spam.eggs> wrote:
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"

:-)

--
Grégoire

May 10 '06 #7
On 2006-05-10, Grégoire Dooms <do******************@AisATandDisDOT.com> wrote:
Ben C wrote:
On 2006-05-09, Ben C <sp******@spam.eggs> wrote:
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".
May 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
2
by: Aravind | last post by:
Hi , This is a Windows form application which interacts with the unmanaged C++ codes . In unmanaged c++ code we allocate around 130MB on the heap for annalysing high resolution images . Earlier...
4
by: RH | last post by:
Hi, I am building a windows application that has a feature that retrieves a set of records when a button is clicked. When the records are being retrieved I experience a complete system...
1
by: Sam | last post by:
I using connected environment via ADO.Net and I wonder how to clear memory once I exit Web Application (ASP.Net). Possible to clear memory whenever perform ADO.Net to load data from SQL Server...
1
by: ABC | last post by:
Will Session automatic clear memory when I unload web page?
6
by: madhu | last post by:
vector<vector<vector<long Vector3D; // 3dvector. for (long k = 0; j < Depth; j++ ) { Vector3D.push_back ( vector<vector<A_Type() ); for (long j = 0; j < Height; j++ ) { Vector3D.push_back (...
2
by: Curious | last post by:
I've found memory leakage in my program that causes the program to be very slow. I rebooted the computer and the program went away. Is there a method in C#.NET that does the same thing as...
3
by: Laser Lips | last post by:
Hi All. I'm building t a User Interface, which populates tables from a database dynamically. It's using a technology called CACHE (pronounced cashai, made by intersystem’s) to get the data form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.