Deleting objects

Terry Reedy tjreedy at
Mon Jan 26 23:29:23 CET 2004

<user at domain.invalid> wrote in message
news:a33a46fe50e2ab07720a1118aa22d576 at
> Say I have a database object 'db',
> that contains table objects, that contain field
> objects so that I can do things like this:
> print db.table.field.value()
> Now, after plundering the database for a while,
> the db objects heads a tree of lots of data.
> If I want to "reset" the object so that I get
> all of my memory back, can I just do:
> del db
> or maybe:
> for t in db.tables:
> del t
> and expect that I have cleaned up all of my
> memory?
> After writing this out, I see that the answer
> is clearly yes, but I will post anyway.


Actually, the issue is not so simple.  'del db' deletes the association
between name db and the database object.  If there is another reference to
the object, that is all that happens.  If not, memory cleanup depends on
the implementation.  CPython will 'free' memory immediately (and
recursively delete tables).  Jython waits for next garbage collection
cycle.  'Free' may just mean available for reuse by Python but not by other

Db objects often have a close method.  You may get more of what you want by
calling that before del.

Terry J. Reedy

More information about the Python-list mailing list