Hi Folks! Sage wants to have unique parent structures, so, Rings should be cached. Using weak references, one can achieve that the cache values may be garbage collected if memory is needed.
Rings usually have a list of generators that are RingElements. RingElements refer to the Ring they generate (their parent). So, Rings naturally give rise to a reference cycle. The garbage collector knows how to deal with reference cycles in most situations. But not in all situations. At http://docs.python.org/library/gc.html I read: --- Objects that have __del__() methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Python to guess a safe order in which to run the __del__() methods. --- My situation now is the following: 1. I'd like to have my Rings cached, in order to fit with the standards of Sage. 2. My Rings need a __del__ method. Namely, each Ring creates a lot of data in the Singular interface; I use the __del__ method for destroying these data, since otherwise they would accumulate in Singular. 3. I can hardly avoid cyclic references. 4. I'd like to allow garbage collection (WeakValueDictionary) for the cache, to save memory. Unfortunately, 2. and 3. makes it impossible to have 4., by the above manual page. What would you recommend in this situation? - Do you see a way to automatically remove the data in the Singular interface when a Ring is deleted, without using a __del__ method? - Do you recommend to check the list gc.garbage and remove things explicitly? - The garbage collector's problem with reference cycles is that it does not know where to start collection. Is there a way (some attribute or so) to declare that some instance of a class is safe (resp. not safe) to be deleted when found in an unreachable reference cycle? - Would you recommend to drop the idea of unique parents with a __del__ method? Best regards, Simon --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---