how to "free" an object/var ?

John Nagle nagle at animats.com
Wed Jan 31 02:30:40 EST 2007


Steven D'Aprano 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) ?

    If your data structure has no backlinks, it will go away
as soon as the last reference to it disappears.  If your
data structure has backlinks, it will hang around until
garbage collection runs.  If your backlinks are
weak references (see "weakref"), the data structure will
be released much sooner.

    If you generate structures with backlinks, like
parse trees, use weak references for all links that point
backwards toward the root, and you'll use less memory.

    In Python, garbage collection is mostly a backup to
the reference counting system.  If your app really
needs periodic GC to run, you're leaking memory.
On servers, this sometimes matters.

				John Nagle



More information about the Python-list mailing list