For me this code is rather unpredictable, as ipython and prompt_toolkit kick in and produce extra objects.
For consistency (at least them the output values are reproducible) it looks better to experiment with Sage's python (./sage --python) for which the code needs to be adjusted, with "^" replaced by "**", and two lines added as the front: from sage.all import * var('x') On Sun, Sep 8, 2024 at 5:03 PM Nils Bruin <nbr...@sfu.ca> wrote: > > This example is definitely leaving loads of stuff on the python heap, so if > there is a leak onto the cython heap then it is not the only one. My guess > would be an interaction with the coercion system or UniqueRepresentation, > which both keeps global weak references to objects. If the key information > ends up containing objects that hold a reference to the values in a weakly > valued dictionary, the garbage collector can not remove the cycle. These > things are hellish to debug. The code below does find the relevant objects on > the heap, though, so you can plot the backwards reference graphs of some of > these objects to see what kinds of links are involved. We have resolved some > of these bugs in the past. > > ``` > import gc > from collections import Counter > > gc.collect() > pre={id(a) for a in gc.get_objects()} > > for A2 in range(1, 1000): > Kn=NumberField(x^2+A2,'w') > m=Kn.class_group().order() > del Kn, m > > gc.collect() > gc.collect() > T=Counter(str(type(a)) for a in gc.get_objects() if id(a) not in pre) > T > ``` > Notes for the code above: > - if you run it twice you get a nearly empty list from T, which is > consistent with UniqueRepresentation objects remaining (they would not be > recreated if they already exist). > - this is confirmed by rerunning the loop but now with another name than 'w' > for Kn. Now new objects do get created! > > On Wednesday 4 September 2024 at 06:09:37 UTC-7 Georgi Guninski wrote: >> >> Probably this shares the same bug as [1] >> >> Calling `NumberField().class_group().order()` in a loop of size N: >> #10^3 leaks: 40.03 MB 40026112 pari= [7950, 1451665] >> #10^4 leaks: 338.49 MB 338493440 pari= [83505, 19297360] >> >> The leak appears to be in the pari heap. >> >> Code .sage: >> ==== >> #Author Georgi Guninski Wed Sep 4 12:58:18 PM UTC 2024 >> #10^3 leaks: 40.03 MB 40026112 pari= [7950, 1451665] >> #10^4 leaks: 338.49 MB 338493440 pari= [83505, 19297360] >> >> import psutil,gc,sys >> >> from sage.all import EllipticCurve >> ps = psutil.Process() >> num=10**3 #10**5 // 2 >> x=var('x') >> def leaknf5(N=10**3): >> gc.collect() >> base = ps.memory_info().rss >> for A2 in range(1, N): >> Kn=NumberField(x^2+A2,'w') >> m=Kn.class_group().order() >> gc.collect() >> mem = ps.memory_info().rss - base >> print(f"{mem/1e6 :.2f} MB ",mem," pari=",pari.getheap()) >> >> leaknf5(10**4) >> ==== >> >> [1]: https://groups.google.com/g/sage-devel/c/fWBls6YbXmw >> Memory leak in |EllipticCurve([n,0]).root_number()| and problem in >> algebraic geometry > > -- > You received this message because you are subscribed to the Google Groups > "sage-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-devel+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sage-devel/d0195683-29e8-4b6a-b81d-3384407a944an%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/CAAWYfq2ROuigdMOizq7NCLZ4y3sF%2BD9OzmqVqsjg6Hc2pJWRHA%40mail.gmail.com.