Yep, I bet coercion is leaking rings. I'll look at this during Sage
days, if not sooner.
- Robert
I can confirm (if there was any doubt) that one can try
{{{
for p in prime_range(upper):
R.<x, y, z> = PolynomialRing(GF(p), 3)
}}}
and watch it eat 800MB of RAM.
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?
Best,
Alex
--
Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne
-- Australia -- http://www.ms.unimelb.edu.au/~aghitza/
We can have uniqueness and collection--it's called weakrefs. I
suspect part of the problem is that the coercion model doesn't use
them enough (there's places I meant to go back to and add them in).
- Robert
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
> On May 3, 12:28 am, simon.k...@uni-jena.de wrote:
>
> Hi,
>
> Is there any reason you opened a new ticket and did not use #5949 as
> mentioned above?
>
>> PS:
>>
>>> One can then do
>>> sage: for p in primes(2,1000000):
>>> ....: R.<x,y,z> = GF(p)[]
>>> sage: get_memory_usage()
>>> 778.35546875
>>
>> And:
>> sage: len(sage.rings.polynomial.polynomial_ring_constructor._cache)
>> 34
>>
>> Hence, indeed, some rings were garbage collected.
>
> I am not sure this fixes the problem mentioned above, but I am still
> testing. It might reduce the memory used already, but I won't know for
> a while.
>
>> Cheers,
>> Simon
>
> As is this patch breaks badly:
Yep, see
http://trac.sagemath.org/sage_trac/ticket/3706
- Robert
Note however that running just a loop where the polynomial ring is
created does benefit from the change in Simon's patch: doing it for p
up to 2^17, this used to eat up 272MB, and after the patch it's only
taking 10MB.
I did experience the same phenomenon that Michael is describing when
running loops where elliptic curves (or even just plane curves) are
created. The patch seems to have no effect on those loops.