[Beginner] delete items of [] also from memory

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 23 04:47:45 EST 2004


Peter wrote:

> "Birgit Rahm" <br_y at yahoo.de> wrote in message
> news:<cns50t$5ip$05$1 at 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.)




More information about the Python-list mailing list