deleting objects by finding all references and deleting?

Terry Reedy tjreedy at
Tue Jun 10 07:52:41 CEST 2008

"Jacob Davis" <j.foster.davis at> wrote in message 
news:C673A5C7-9971-4CAA-8CEB-3993C3E93F9C at
| I read in some earlier messages that an object in Python is only
| removed or freed from memory when all references to that object have
| been deleted.  Is this so?

A Python interpreter *may* delete an object when, but only when, it becomes 
inaccessible from the currently running program.  What interpreters do 
depends on the interpreter.

| If so, is there a way to get all of the references to an object, so
| that they may all be deleted, thus actually deleting the object?

'References' are implementation details, not Python objects.  So while in 
CPython, sys.getrefcount(ob) returns a count, the meaning of 'get all 
references' is not obvious.

So try to create, process, and delete objects as you go, and repeat for the 
next, instead of create all, process all, and delete all.  For CPython, 
look at the gc module.


More information about the Python-list mailing list