Memory usage strictly increasing on Sage interactive shell
Hello,
I executed the following script on the Sage interactive shell on Ubuntu 18.04 (on SageMath version 8.7.beta5):
sage: import gc
sage: t = 100000
sage: l = [n for n in xrange(t,t+100)]
sage: ll = []
sage: for x in l:
....: s = 0
....: for n in xrange(0,x):
....: s += cos(RDF(2)*RDF(pi)*RDF(n)/RDF(x))
....: ll.append([x, s])
....: del(s)
....: print "memory usage: " + str(get_memory_usage()) + ", gc: " + str(gc.collect())
....:
memory usage: 5824.5546875, gc: 277
memory usage: 5828.3046875, gc: 0
memory usage: 5832.0546875, gc: 0
memory usage: 5836.0546875, gc: 0
:
memory usage: 6203.5546875, gc: 0
memory usage: 6207.5546875, gc: 0
As we can see the memory usages, it increases by about 4 MB per each step. Thus, after completed, it increased around 400 MB in total. It is frustrating since it eventually eats up all memories in my system as the size of the input list grows and gc.collect() does not seem to help.
I suspect that there were memory leaks or Sage(or Python) stored all the values of cos() and never freed them for some reasons while the shell not killed.
Can someone please explain what was going on and suggest how to avoid this unwanted memory consumption? Thank you in advance.
I think the problem is not with
cos
but rather withpi
:The memory leak goes away when one replaces
RDF(pi)
by e.g.RDF(3.14)
orRDF.pi()
. Something must be going wrong when callingRDF(pi)
. The same memory leak occurs when replacing thepi
inRDF(pi)
with any of the other mathematical constants.You're absolutely right! Thank you very much for your comment. If I use
pari(pi)
, it works without memory leaks. However, ifgp(pi)
used, it just hangs. I still want to know how the leaks happen; I guess that this issue is related withgp('znprimroot(10007)')
andpari('znprimroot(10007)')
shown on Interfaces--Sage Tutorial v86. By the way, if you put it in Answer, I will mark yours as an answer. Best, jb.If you wait long enough (or run it on a faster computer) then it will work without leaks with
gp(pi)
as well. I will report the memory leak and post that as an answer.