[Beginner] delete items of [] also from memory

B

Birgit Rahm

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?
 
B

Birgit Rahm

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
problem. So I asked.
Is del A[0:] the better or an equal solution?
Birgit
 
E

Eric Brunel

Birgit said:
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
problem. So I asked.
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
 
F

F. Petitjean

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
problem. So I asked.
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.
 
S

Steve Holden

Birgit said:
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
problem. So I asked.
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
 
B

Birgit Rahm

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
 
P

Peter

Birgit Rahm said:
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.
 
D

Duncan Booth

Peter said:
Birgit Rahm said:
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.)
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top