473,948 Members | 1,526 Online

[Beginner] delete items of [] also from memory

Hello newsgroup,

I am a beginner, so I am asking maybe immoderate questions.
I want to delete a [] variable, after filling it with complex objects. How
should I do it?
e.g.
AAA = []
AAA = [B, C, D, E, F]
Can I
1) AAA = []
2) del AAA[0:]
3) ?
whats about the computers memory, will it get free? Or will it get more and
more while Python is running?
--

Mit freundlichen Grüßen / best regards
Birgit Rahm
Jul 18 '05 #1
9 2632
Hi !

But... And if you try ?
Jul 18 '05 #2
> But... And if you try ?
I tryed A = [].
And I think thats why my RAM is filling up. But I dont know if this is the
Is del A[0:] the better or an equal solution?
Birgit

Jul 18 '05 #3
Birgit Rahm wrote:
But... And if you try ?

I tryed A = [].
And I think thats why my RAM is filling up. But I dont know if this is the
Is del A[0:] the better or an equal solution?
Birgit

Not exactly equal, but it depends on the context. Consider:

a = [None] * 10000
b = a
a = []

The first line creates a 10000 element list and adds a reference to it ("a");
the second adds a new reference to the list ("b"); the third removes a reference
from the list and makes it point to a new empty list. So the 10000 element list
still has a reference ("b"), and is not deleted from memory, since it can still
be accessed via the variable b.

OTOH, consider this:

a = [None] * 10000
b = a
del a[:]

The two first lines are the same, but the third one does not just delete a
reference to the 10000 element list; it actually deletes all elements from the
list itself. So now, a *and* b point on the empty list, and the memory used by
the original list is given back to the system (or should be... see further)

What makes you think the memory is not given back to the system? Does the memory
occupied by your script increase and increase, or do you just see the memory
occupied by your script remain constant when you do the del a[:]? In the latter
case, it may be perfectly normal: it is quite frequent that the memory freed by
an operation is not returned immediatly to the system, but kept for future use
by the program.

BTW, I tried the following script, watching the memory consumption at each line:
a = [None] * 10000
a = []
b = [None] * 10000
b = []
c = [None] * 10000
c = []
d = [None] * 100000

The first line actually increase the memory allocated for the Python
interpreter; the second one doesn't decrease it, but the third one does not
increase it, because the memory occupied by the first 10000 element list was
re-used for the second one. Same thing for the third 10000 element list (the one
put in c). The last line *did* increase the consumed memory, because more space
was needed for the 100000 element list.

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
Jul 18 '05 #4
On Mon, 22 Nov 2004 11:56:26 +0100, Birgit Rahm <br**@yahoo.d e> wrote:
But... And if you try ?

I tryed A = [].
And I think thats why my RAM is filling up. But I dont know if this is the
Is del A[0:] the better or an equal solution?
Birgit

What about A[:] = None ?

PS I have not seen the parent message, so take this advice with a handful
of salt.
Jul 18 '05 #5
Birgit Rahm wrote:
But... And if you try ?

I tryed A = [].
And I think thats why my RAM is filling up. But I dont know if this is the
Is del A[0:] the better or an equal solution?
Birgit

The two solutions are pretty much equal, the only (insignificant)
difference being that a new empty list is created when you use A = [].

In either case the memory occupied by the elements of the list will all
become available for garbage collection, and hence for the creation of
new Python objects.

It will *not*, however, be returned to the operating system, so your
process's memory footprint will remain at the high water mark set when
you were using maximum memory for referenced objects.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #6
What makes you think the memory is not given back to the system? Does the memory occupied by your script increase and increase, or do you just see the memory occupied by your script remain constant when you do the del a[:]? In the latter case, it may be perfectly normal: it is quite frequent that the memory freed by an operation is not returned immediatly to the system, but kept for future use by the program.

ok, I think that helped me to understand what's going on in my program and
why the memory is not freed immediately. I will take del A[0:].
Thanks to all, Birgit
Jul 18 '05 #7
"Birgit Rahm" <br**@yahoo.d e> wrote in message news:<cn******* ******@news.t-online.com>...
Hello newsgroup,

I am a beginner, so I am asking maybe immoderate questions.
I want to delete a [] variable, after filling it with complex objects. How
should I do it?
e.g.
AAA = []
AAA = [B, C, D, E, F]
Can I
1) AAA = []
2) del AAA[0:]
3) ?
whats about the computers memory, will it get free? Or will it get more and
more while Python is running?

I too am a novice, so am prepared to be corrected if this is wrong.

I don't think you need to worry - Python handles memory allocation
automatically. Names are not specifically allocated to memory, and
when the last reference to a name is deleted all the associated memory
is freed.
Jul 18 '05 #8
F. Petitjean wrote:
What about A[:] = None ?

I don't think that does what you want it to:
a = [None]*100
a[:] = None

Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
TypeError: can only assign an iterable

Steve
Jul 18 '05 #9
Peter wrote:
"Birgit Rahm" <br**@yahoo.d e> wrote in message
news:<cn******* ******@news.t-online.com>...
Hello newsgroup,

I am a beginner, so I am asking maybe immoderate questions.
I want to delete a [] variable, after filling it with complex
objects. How should I do it?
e.g.
AAA = []
AAA = [B, C, D, E, F]
Can I
1) AAA = []
2) del AAA[0:]
3) ?
whats about the computers memory, will it get free? Or will it get
more and more while Python is running?

I too am a novice, so am prepared to be corrected if this is wrong.

I don't think you need to worry - Python handles memory allocation
automatically. Names are not specifically allocated to memory, and
when the last reference to a name is deleted all the associated memory
is freed.

It depends on what the original poster meant by 'complex' objects.

Objects with circular references may not be freed until the garbage
collector kicks in, and objects with circular references and a __del__
method may not be freed at all. However, provided you avoid writing __del__
methods on such objects, the memory may not be freed immediately but it
will be freed eventually.

Objects which are not involved in any cyclic references will be freed, as
you say, as soon as the last reference goes.

('Freed' in this context means released for reuse by Python --- don't ever
expect the process to release memory back to the rest of the system.)

Jul 18 '05 #10

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