how to "free" an object/var ?

Paddy paddy3118 at netscape.net
Wed Jan 31 08:22:52 CET 2007


On Jan 31, 6:52 am, Steven D'Aprano <s... at REMOVEME.cybersource.com.au>
wrote:
> 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

The folowing will make the data available for garbage collection no
matter what references it:

>>> l = ["data"] *10
>>> l
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data', 'data']
>>> l2 = l
>>> l[:] = []
>>> l2
[]
>>>

- Paddy.




More information about the Python-list mailing list