CPython's cyclic garbage collector (was [Python-ideas] Automaticcontext managers)

C

Chris Angelico

[[ Resending under a more appropriate subject line... sorry about
that, ignore the other one as it'll only confuse matters ]]

The problem is in knowing in which order the objects should be
collected.

For example, if A refers to B and B refers to A, should you collect A
then B, or B then A? If you collect A first, then, for a time, B will
be referring to a non-existent object. That's not good if the objects
have destructors which need to be run.

Spin-off thread from python-ideas to discuss a more general question
of garbage collection of cyclic structures.

Once it's been proven that there's an unreferenced cycle, why not
simply dispose of one of the objects, and replace all references to it
(probably only one - preferably pick an object with the fewest
references) with a special temporary object? In fact, that could
probably be done in CPython by wiping out the object in memory and
replacing it with a special marker of some sort, which would then
automatically "take over" all references to the old object. Any
attempt to manipulate this object could simply pop back with a
DestructedObject exception or something.

Is this a plausible (never mind viable yet, just conceptually
plausible) alternative to sticking them into gc.garbage and ignoring
them? It'd allow a double-linked list/tree to function cleanly -
imagine, for instance, something like the DOM facilities available to
web browser scripts:

class DOMObject:
def __init__(self,parent):
self.parent=parent
self.firstchild=self.sibling=None
if not parent: return
if not parent.firstchild:
parent.firstchild=self
else:
child=parent.firstchild
while child.sibling:
child=child.sibling
child.sibling=self
def __del__(self):
print("Disposing of id #%d"%id(self))

document=DOMObject(None)
body=DOMObject(document)
p=DOMObject(body)
p=DOMObject(body)
p=DOMObject(body)
del document,body,p
gc.collect()

The __del__ method would need to clean up the external resources used
by this object, but wouldn't have to walk the tree. Yet, just because
there is a reference loop and there are __del__ methods, the garbage
collector gives up and leaves it to the program author to deal with.

I can understand if this is considered too complicated and too unusual
a circumstance to be worth bothering to support, but I'm curious as to
whether it's at least conceptually reasonable to do something like
this.

ChrisA
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top