On May 2, 2009, at 11:48 PM, [email protected] wrote:

>
> Hi!
>
> On 3 Mai, 05:42, Alex Ghitza <[email protected]> wrote:
> ...
>> I can see good reasons why we want the current behaviour of
>> "uniqueness of parents", and what we just saw here is a good reason
>> why we might *not* want it.  How can we reconcile this?  Can we make
>> it easy to turn off "uniqueness of parents" in some situations?
>
> In a dirty way, certainly: The polynomial rings constructed with
> PolynomialRing are cached at
>    sage.rings.polynomial.polynomial_ring_constructor._cache
>
> So, just delete appropriate items in that cache.
>
> Robert mentioned weak references. Does it mean the following?
> - When creating a reference to a polynomial ring in that cache, then
> tell the garbage collector that this reference should perhaps delay
> the deletion of the polynomial ring, but it should not prevent
> deletion.
> - When there is any other reference to the polynomial ring, then this
> counts as a proper reference, preventing the garbage collection.
>
> Hence, when you do
>   sage: R=QQ['x']
> then there is a weak reference in cache and a proper reference ``R``
> to QQ['x']. So, the ring won't be garbage collected. When you continue
>   sage: R=GF(3)['x']
> then only the weak reference points to QQ['x']. So, the ring may be
> garbage collected.
>
> I think in this way one would have unique parents *and* would fix the
> memory leak, but I am afraid I don't know enough about weak references
> to implement it.

Yes, that is exactly how it works. We should be using a  
weakref.WeakValueDictionary (see http://docs.python.org/library/ 
weakref.html).

Perhaps changing line 39 of  
sage.rings.polynomial.polynomial_ring_constructor from

_cache = {}

to

_cache = weakref.WeakValueDictionary

would be enough. For some reason, weakrefs used to be used, but  
that's been disabled.

http://hg.sagemath.org/sage-main/diff/b4f33dc4908b/sage/rings/ 
polynomial/polynomial_ring_constructor.py

I think the underlying issue has been fixed. Note, however, that  
there may also be caching elsewhere (e.g. I suspect a spot or two in  
the coercion model).

- Robert



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to