Deleting objects

U

user

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.

Thanks

Toby
 
J

Jeff Epler

On Mon, Jan 26, 2004 at 08:04:16PM +0000, (e-mail address removed) wrote:
[Can I execute]
for t in db.tables:
del t

and expect that I have cleaned up all of my
memory?

The above code clearly doesn't "free" anything, because there are still
references to each of the values 't' has at deletion---namely, the entry
in db.tables!

Jeff
 
G

Gerrit Holl

If I want to "reset" the object so that I get
all of my memory back, can I just do:

del db

This may work, if you don't have other references.
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.

The answer is no, really :)

'del' only removes a certain name from the current namespace. An object
is only destroyed when *all* references to it have been destroyed.
When you are looping over a certain collection, each time the body of
the loop is executed, the loop variable (here 't') gets redefined. So if
the referencecount to a certain object (db.tables[x]) is N, it will
become temporarily N+1 when looping over it: with 'del t', you destroy
the local name, so it becomes N again. Conclusion: This aint gonna work.

Because of this, it is generally better to explicitly kill/destroy/close
something. I don't know anything about databases, but maybe you want to
..close() it? 'del db' may work, but can be tricky if other objects, like
dicts, lists, sets or others, contain (nameless) references to the
object.

yours,
Gerrit.
 
T

Terry Reedy

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.

Why?

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
programs.

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

Terry J. Reedy
 
T

Terry Reedy

As Gerrit said, this is equivalent to 'pass'.

db.tables = [] # or
db.tables[:] = [] # or
db.cleartables()

is more likely to work. But a db.close() method should contain what will
work.

TJR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,501
Latest member
log5Sshell/alfa5

Latest Threads

Top