how to "free" an object/var ?

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Wed Jan 31 01:52:12 EST 2007


On Tue, 30 Jan 2007 15:48:37 -0800, James Stroud wrote:

> Stef Mientki wrote:
>> If I create a large array of data or class,
>> how do I destroy it (when not needed anymore) ?
>> 
>> Assign it to an empty list ?
>> 
>> thanks,
>> Stef Mientki
> 
> It will be gc'd when you leave the scope or you can call del() to 
> explicitly get rid of the object if its existence bothers you.

That is not quite correct.

big_list = ["data"]*1000000
another_reference = big_list
del big_list

At this point, the list of one million "data" strings still exists.

del big_list doesn't delete the list object, it removes the name
"big_list". Then, only if the list has a reference count of zero, Python
will dispose of the object and free the memory (if your OS allows that).
If there are still references to it, like "another_reference" above, it
will not be disposed of.

As far as I know there is no way to force the deletion of an object even
if it is in use. This is a Good Thing.


-- 
Steven D'Aprano 




More information about the Python-list mailing list